Skip to:
Content
Pages
Categories
Search
Top
Bottom

Add content to first reply only


  • mattbru
    Participant

    @mattbru

    wp v5.0.1
    bbpress v2.5.14

    How can I hook into the FIRST reply on a single topic page? I want to add an image attachment only to the first reply, but i cannot find how to do so. I hooked into the action “bbp_theme_before_reply_content“, but it adds the image to every reply on the page!

    I thought about getting a list of all reply ids, then comparing the current reply id (bbp_get_reply_id()) with the first id in the array, and showing content if they are equal. That would solve the problem. BUT I also failed at getting the list of ids – I tried with bbp_get_all_child_ids

    I also wish the FIRST reply in the loop would have a unique id or class, but it does not 🙁

    Here is the page:

    Meet My New Crested Gecko

Viewing 4 replies - 1 through 4 (of 4 total)

  • Robin W
    Moderator

    @robin-w

    ok, get the topic ID, and then in your hook to bbp_theme_before_reply_content look up whether the topic has had any replies by using

    $count = get_post_meta ($topic_id, '_bbp_reply_count' , false)

    If $count=0, then there have been no replies, so this is the first.


    mattbru
    Participant

    @mattbru

    Thank you Robin W. But that does not work.

    $count = get_post_meta($topic_id, '_bbp_reply_count', false);
    is the same as
    $count = bbp_get_topic_reply_count($topic_id);
    Except the second one returns a number, first one gives you an array.
    The results of what you suggested gives me array([0]=>6) , meaning there are six replies to the topic.

    If you do a conditional like this: if ($count === 0) { // then code } , its only going to run the code if there are no replies to the post. That’s not what I need.


    mattbru
    Participant

    @mattbru

    Okay, I GOT IT!
    The key is to compare the topic id and the reply id. If they are equal, then its the first reply in bbpress.

    Here is my full solution:

    <?php
    function add_content_before_first_reply_content($content,$id){
    
    	$post = get_post($id);
    	$topic_id = bbp_get_topic_id();
    	$reply_id = bbp_get_reply_id();
    	$content_to_add = '';
    
    	if ($topic_id === $reply_id) {
    		// do whatever you need to in  here
    		$content .= $content_to_add;
    	}
    
    	return $content;
    
    }
    add_filter( 'bbp_get_reply_content', 'add_content_before_first_reply_content', 99, 2 );

    Robin W
    Moderator

    @robin-w

    great – glad you are fixed, and thanks for posting the solution

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar