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 7 years, 2 months ago.
- AuthorPosts
- December 21, 2015 at 1:41 pm #7133
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
December 28, 2015 at 4:20 pm #7161Hi!
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 :)
December 28, 2015 at 5:12 pm #7162Can 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
December 28, 2015 at 5:21 pm #7163Indeed, 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 :)
December 28, 2015 at 5:44 pm #7164Yep, 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 7 years, 2 months ago by
somuch77.
December 28, 2015 at 5:48 pm #7166Actually, yes, and it’s probably better that way. Try changing the whole thing to this:
Best,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; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
December 28, 2015 at 6:13 pm #7167Yep, Its done 😀 Thanks for your help!
-
This reply was modified 7 years, 2 months ago by
- AuthorPosts
You must be logged in to reply to this topic.