Home › Forums › Product Support Forums › Related Posts Pro for WordPress Support › How to display post's category on related posts
- This topic has 6 replies, 2 voices, and was last updated 10 years, 5 months ago by
somuch77.
-
AuthorPosts
-
December 21, 2015 at 1:41 pm #7133
somuch77
ParticipantHi 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 #7161Ernest Marcinko
KeymasterHi!
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.
December 28, 2015 at 5:12 pm #7162somuch77
ParticipantCan 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 #7163Ernest Marcinko
KeymasterIndeed, 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 🙂
December 28, 2015 at 5:44 pm #7164somuch77
ParticipantYep, 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
somuch77.
December 28, 2015 at 5:48 pm #7166Ernest Marcinko
KeymasterActually, 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]December 28, 2015 at 6:13 pm #7167somuch77
ParticipantYep, Its done 😀 Thanks for your help!
-
This reply was modified 10 years, 5 months ago by
-
AuthorPosts
- The forum ‘Related Posts Pro for WordPress Support’ is closed to new topics and replies.