codestars (@codestars)

Forum Replies Created

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

  • codestars
    Participant

    @codestars

    I can’t edit my previous post so here is another update.
    Using bbp_get_reply_position_raw() instead of bbp_get_reply_position() works better for me.
    In this case it’s better to use bbp_get_replies_per_page() instead of bbp_get_topics_per_page().
    And finally, remove the line with $position and just use $position_raw, because it already returns an integer.


    codestars
    Participant

    @codestars

    Thank you alriknijdam, this was exactly what I needed.

    I improved your code a bit, for example you can use bbp_get_topics_per_page() so the amount of replies per page will always be correct. And you can use bbp_get_root_slug() to dynamically get the root URL of where your forum is located.

    /**
     * Get reply in topic permalink.
     *
     * @param int $reply_id
     * @param int $topic_id
     *
     * @return string
     */
    function custom_get_reply_in_topic_permalink( $reply_id, $topic_id ) {
    
    	$position_raw = bbp_get_reply_position( $reply_id, $topic_id );
    	$position     = str_replace( ',', '', $position_raw );
    	$page_raw     = ceil( $position / bbp_get_topics_per_page() );
    	$page         = $page_raw > 1 ? '/page/' . $page_raw : '';
    
    	$slug = get_post_field( 'post_name', get_post( $topic_id ) );
    
    	return bbp_get_root_slug() . '/topic/' . $slug . $page . '/#post-' . $reply_id;
    }

    Can be used like this:
    <a href="<?php echo custom_get_reply_in_topic_permalink( $reply_id, $topic_id ); ?>">

    Enjoy!

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