This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

How to display post's category on related posts

Home Forums Product Support Forums Related Posts Pro for WordPress Support How to display post's category on related posts

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #7133
    somuch77somuch77
    Participant

    Hi Ernest

    On RPP option panel there’s option to display author or date of posts on RPP. I can’t find a way how to display categories instead.

    I tried to open on shortcode php, I found code to echo author <?php echo $rpp_post->author; ?>
    I tried to call categories from here, tried some code like $rpp_post->category or $rpp_post->categories and some others code, none of them work. Is there a way to echo related posts category?

    Thanks

    #7161
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi!

    I think a better solution would be to use a filtering function, so the source code is not changed and it’s update proof.

    I’ve put together a function that might do the trick. Put this to the functions.php file in your active theme directory:

    [php]add_filter( ‘rpp_item_after_postprocessing’, ‘rpp_add_category_titles’, 1, 1 );

    function rpp_add_category_titles( $post ) {

    // Get the post categories
    $post_categories = wp_get_post_categories( $post->ID );
    $cats = "";

    // Concatenate category names to the $cats variable
    foreach($post_categories as $c){
    $cat = get_category( $c );
    $cats .= " ".$cat->name;
    }

    if ( $cats != "")
    $post->author = $cats;

    return $post;
    }[/php]

    Make sure to save the related posts pro options to clear the posts cache.

    I haven’t tried this code, but I’m hoping it will work.

    #7162
    somuch77somuch77
    Participant

    Can you check if this actually works?

    I already add that code on my theme’s function.php, not working in here.

    Am I have to edit other than function.php? I tried to change
    <?php echo $rpp_post->author; ?>
    to
    <?php echo $rpp_post->cats; ?>
    on \includes\views\rpp.shortcode.php
    also not working.

    Thanks

    #7163
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Indeed, I tested it and there was an error. Change line:

    [php]$post_categories = wp_get_post_categories( $post->ID );[/php]

    to

    [php]$post_categories = wp_get_post_categories( $post->id );[/php]

    and it should start working 🙂

    #7164
    somuch77somuch77
    Participant

    Yep, it works! Is it possible to add comma in between categories if the post have 2 or more categories?
    Maybe like this:
    Health, Sport
    Health, Sport, Fitness

    • This reply was modified 10 years, 5 months ago by somuch77somuch77.
    #7166
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Actually, yes, and it’s probably better that way. Try changing the whole thing to this:

    [php]add_filter( ‘rpp_item_after_postprocessing’, ‘rpp_add_category_titles’, 1, 1 );

    function rpp_add_category_titles( $post ) {

    // Get the post categories
    $post_categories = wp_get_post_categories( $post->id );
    $cats = array();

    // Concatenate category names to the $cats variable
    foreach($post_categories as $c){
    $cat = get_category( $c );
    $cats[] = $cat->name;
    }

    if ( count($cats) )
    $post->author = implode(", ", $cats);

    return $post;
    }[/php]

    #7167
    somuch77somuch77
    Participant

    Yep, Its done 😀 Thanks for your help!

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Related Posts Pro for WordPress Support’ is closed to new topics and replies.