366 posts
  • Has referred 50+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+5 more
PixelStores says

Hey,

I have a function that echoes the number of posts a topic has, it basically echoes a number. The function is topic_posts();

This is what i’m trying to do with the function:

function checkTopic(){
$topic_count = topic_posts();

    if ( $topic_count == "1" ) {
        echo "Started";
    } else {
        echo "Reply";
    }
}

checkTopic();

The result i get is 1Reply

Any ideas?

Matt

3256 posts
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
ParkerAndKent says

Your topic_posts func shouldn’t echo, but return the number string.

Parker

366 posts
  • Has referred 50+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+5 more
PixelStores says

Your topic_posts func shouldn’t echo, but return the number string. Parker

Thanks for the reply, could you elaborate in what you mean “return the number string” I want my function checkTopic(); to see if the topic_posts function displays 1, and if so echo Started..e.t.c.

Cheers

3256 posts
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
ParkerAndKent says


Your topic_posts func shouldn’t echo, but return the number string. Parker

Thanks for the reply, could you elaborate in what you mean “return the number string” I want my function checkTopic(); to see if the topic_posts function displays 1, and if so echo Started..e.t.c.

Cheers

function topic_posts() {

return "1";

}

Use return, not echo ;)

Then, if you return a number (return 1; instead of return “1”;) your if should check ($topic_count == 1)

3714 posts Community Moderator
  • Helps us moderate the forums
  • Contributed a tutorial on Tuts+
  • Located in Netherlands
  • Made it to the Authors' Hall of Fame
+9 more
Joost Moderator says

That “1” is definitely not coming from the checkTopic function, so unless there’s something else running there I’d say the topic_posts() function echo’s the 1 somehow.

Also, I guess it gives “Reply” instead of “Started” because you’re checking for a string in your if-statement while topic_posts will probably be returning an integer value.

Have a go at changing:
if ( $topic_count == "1" ) {
into
if ( $topic_count == 1 ) {

EDIT : Nevermind this post, P&K is right – that’s probably the problem. ;)

81 posts
  • Has been part of the Envato Community for over 5 years
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
Fynn says

You’re function shouldn’t echo, but return true or false. Than you can do:

If(checkTopic()) { echo 'Started'; }
366 posts
  • Has referred 50+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+5 more
PixelStores says

@p&k

Yes that makes perfect sense to me and your probably right, i only have one problem now is changing the topic_posts() function, this function is part of bbPress. Looking through the core files i’m having difficulty on whats going on.

Heres the functions reference:

http://phpxref.ftwr.co.uk/bbpress/nav.html?_functions/index.html

I did a page search and found the function a number of times and ended up with

 function topic_posts( $id = 0 ) {
1184      echo apply_filters( 'topic_posts', get_topic_posts( $id ), get_topic_id( $id ) );
1185  }

...?

@Joost

I gave this ago and still the same output 1Reply.

3256 posts
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
ParkerAndKent says

Ok, seen the docs:

function topic_posts( $id = 0 ) {
      echo apply_filters( 'topic_posts', get_topic_posts( $id ), get_topic_id( $id ) );
}

function get_topic_posts( $id = 0 ) {
      $topic = get_topic( get_topic_id( $id ) );
      return apply_filters( 'get_topic_posts', $topic->topic_posts, $topic->topic_id );
}

The get_topic_posts returns the value, so that’s the right function to use:

function checkTopic(){
$topic_count = (int)get_topic_posts();

    if ( $topic_count == 1 ) {
        echo "Started";
    } else {
        echo "Reply";
    }
}

I removed the quotes from “1” and casted as int the $topic_count var, so it must work now.

Not sure if get_topic_posts returns a number, so just do an echo of it to see what it returns… because if it returns an array you should then use the count to valorize $topic_count, so it would be:

$topic_count = count(get_topic_posts());

Parker

366 posts
  • Has referred 50+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+5 more
PixelStores says

Wow! Thanks Parker works like charm :)

One question, how did you know the get_topic_posts(); function returns the values for the topic_posts() function, the name is a clue, but trying to understand the code, would it be of this

return apply_filters( 'get_topic_posts', $topic->topic_posts, $topic->topic_id )

Also, what does the (int) mean, (int)get_topic_posts();

Thanks again!

3256 posts
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
ParkerAndKent says

Hi,

you’re welcome.

Well, usually (like in wp) the functions come with 2 versions, one that echo the value and one that returns it. The one that returns it usually have the get_ before the function name… so I checked the bb docs and found it :)

The (int) before a value forces it to be an integer (number). Check “php type casting” on google for more info.

I did that because I don’t know bb, so not being sure about what get_topic_posts returns I put (int) to be sure to have an integer and verify the if statement (== 1).

Parker

by
by
by
by
by
by