1

In this case I want to make the code work only if there are more than 5 replies.

<?php if ( bbp_topic_reply_count() > 5 ) : ?>
    <?php query_posts('gdsr_sort=thumbs&post_type=bbp_reply&posts_per_page=2&post_parent='.$post->ID); ?>
    <?php while ( have_posts() ) : the_post(); ?>
        <h2><?php  the_title(); ?></h2>
        <?php the_content(); ?>
        <?php bbp_reply_author_link( array( 'type' => 'avatar' ) ); ?>
        <?php bbp_reply_author_link( array( 'type' => 'name' ) ); ?>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
<?php endif; ?>

The replies are effectively being shown in the code below:

<h4><?php bbp_topic_reply_count(); ?></h4>

But it seems like its not working in the if statement.

Any suggestions?

Peter Lindqvist
  • 10,122
  • 3
  • 41
  • 60
alexchenco
  • 53,565
  • 76
  • 241
  • 413
  • What is the return value of `bbp_topic_reply_count()`? Is it a number, or is it some HTML code which contains the number somewhere inside with formatting etc. If it is someting like `
    5
    ` then it obviously won't get you the number 5 in an `if` statement. - Espacially so if it **prints out** the result instead of `return`ing it.
    – vbence Mar 24 '11 at 09:59
  • But you accepted Matt's answer which says exactly the oppsoite. – vbence Mar 24 '11 at 10:11

4 Answers4

5

Try using:

if ( bbp_get_topic_reply_count() > 5)

As with many templatey functions in various PHP libraries, there are two variants of this function. One, bbp_topic_reply_count(), automatically echoes the count, rather than returning it. The other, bbp_get_topic_reply_count() actually returns the value to you rather than echoing it.

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
  • Thanks! Are you familiar with bbPress or this is a standard way of making PHP functions? – alexchenco Mar 24 '11 at 10:02
  • 1
    @alexchenco I'm not familiar with bbPress, but this kind of behaviour and naming is very common in template libraries; I think I first came across it in both WordPress and ZenPhoto. It saves you having to `echo` things all over the place, while still letting you get hold of the values that the templates are echoing if you need to. So I just guessed the name that the "get" function would have and Googled it to make sure it existed before I answered :) – Matt Gibson Mar 24 '11 at 10:05
3

May I suggest using

if (bbp_get_topic_reply_count() > 5):

The reason for this is that the function bbp_topic_reply_count() does not return the count value, instead it outputs this value. So when you compare the return value of bbp_topic_reply_count it is null and this produces the following statement

if (0 > 5) :

Which of course is always false.

Peter Lindqvist
  • 10,122
  • 3
  • 41
  • 60
  • np, I found out here http://phpdoc.ftwr.co.uk/bbpress-plugin/bbPress/TemplateTags/_bbp-includes---bbp-topic-template.php.html#functionbbp_get_topic_reply_count – Peter Lindqvist Mar 24 '11 at 10:08
0

I don't really know the coding conventions of WordPress but I'm sure you don't have to open and close the php tag <?php in every line.

vbence
  • 20,084
  • 9
  • 69
  • 118
0

bbp_topic_reply_count() isn't returning the reply count. It is only echoing it. This means you can't use it as a comparison because the function doesn't return a number to compare against. I am not familiar with the bbpress functions, but you will have to find an alternative.

DingoEatingFuzz
  • 623
  • 3
  • 11