Hi dominmax
Do you want the current post at the top of the related posts list?
This will add the current post at the top of the related posts list. Put in in your (child) theme’s functions.php file.
add_filter( 'related_posts_by_taxonomy', 'my_theme_related_posts_add_current', 10, 4 );
function my_theme_related_posts_add_current( $results, $post_id, $taxonomies, $args ) {
// Check if it's related posts for a widget or shortcode.
if ( ! in_array( $args['type'], array( 'widget', 'shortcode' ) ) ) {
return $results;
}
// Check if fields is empty (not used by widget or shortcode).
if ( $args['fields'] ) {
return $results;
}
// Get the current post.
$curr_post = get_post( $post_id );
// Check if current post and related posts are found.
if ( $curr_post && ! empty( $results ) ) {
// Add the relatedness score to the current post.
// Could be usefull if needed later in the process.
$count = count( $args['related_terms'] );
$curr_post->score = array( $count, 0 );
$curr_post->termcount = $count;
// Add current post to the related posts.
array_unshift( $results, $curr_post );
}
return $results;
}
btw:
consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost. Or create a plugin with the code above.
Thank you keesiemeijer,
The code works properly.
My point is to bold or stylize the active element and keep (if it is not too complicated) the order of elements.
– before adding your code after choosing B the list looked like:
A
C
D
– after adding your code it gives:
B
A
C
D
And maybe is it possible to have
-either:
A
B class=”choosen-li”
C
D
-or at least (as an extension of your code to add a class):
B class=”choosen-li”
A
C
D
Thank you!
Dominik
Hi,
Maybe my previous question is too confusing…
So, short question, is there a way to add a class to the current post at the related posts list?
Thank you in advance for help.
Dominik