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

This topic contains 6 replies, has 2 voices, and was last updated by somuch77 somuch77 8 years, 3 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #7133
    somuch77
    somuch77
    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 Marcinko
    Ernest 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:

    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;
    }

    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.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #7162
    somuch77
    somuch77
    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 Marcinko
    Ernest Marcinko
    Keymaster

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

    $post_categories = wp_get_post_categories( $post->ID );

    to

    $post_categories = wp_get_post_categories( $post->id );

    and it should start working 🙂

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #7164
    somuch77
    somuch77
    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 8 years, 3 months ago by somuch77 somuch77.
    #7166
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

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

    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;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #7167
    somuch77
    somuch77
    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.