• Resolved dominmax

    (@dominmax)


    Hi, I am using the plugin as widget. Is this possible to show on the widget list – the title of the active post? Currently, when I click one of the titles it goes to the chosen title and hides the chosen title on the widget list. E.g.: http://nowa.docelowo.pl/barcelona/samolot.html
    I would rather bold the chosen title than hide it. Is it possible to achieve this?

    Best for you
    Dominik

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author keesiemeijer

    (@keesiemeijer)

    Hi dominmax

    Do you want the current post at the top of the related posts list?

    Plugin Author keesiemeijer

    (@keesiemeijer)

    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.

    Thread Starter dominmax

    (@dominmax)

    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

    Thread Starter dominmax

    (@dominmax)

    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

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Show active post title on widget’ is closed to new replies.