• Resolved elahwyhw23

    (@elahwyhw23)


    Hi!

    I just installed WPCode because I hoped a code snippet could exclude a category from navigation on single posts. Currently previous & next posts include posts from multiple categories. I would like previous and next posts to include just posts in the “blog” category. Is excluding categories possible with WPCode?

    Possible functions involved include exclude_category, infinite_get_post_option, and previous/next_post_link. My website uses a child theme of Infinite.

    Thank you!

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @elahwyhw23,

    What you are describing should be possible with a custom snippet in WPCode.

    You can try something like the code below (replace with your category ID) but be aware that if a post has 2 categories and one of them is blog it will still be excluded.

    add_filter( 'get_next_post_excluded_terms', 'wpcode_custom_exclude_all_terms_but_blog' );
    add_filter( 'get_previous_post_excluded_terms', 'wpcode_custom_exclude_all_terms_but_blog' );
    
    function wpcode_custom_exclude_all_terms_but_blog( $term_ids ) {
    	$excluded_terms = get_categories(
    		array(
    			'hide_empty' => false,
    			'fields'     => 'ids',
    			'exclude'    => array(
    				1, // Replace with the id of the category you want to include.
    			),
    		)
    	);
    
    	if ( empty( $term_ids ) ) {
    		$term_ids = array();
    	}
    
    	return array_merge( $term_ids, $excluded_terms );
    }
    Thread Starter elahwyhw23

    (@elahwyhw23)

    Hi @gripgrip,

    Thank you very much for this code. I replaced “1” with the appropriate category that I want to see, so now just posts from that category are in the previous & next links. I’m awaiting leadership feedback to ensure that this is what they want.

    Thank you again!

    • This reply was modified 2 years, 3 months ago by elahwyhw23.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Is single post navigation possible with this plugin?’ is closed to new replies.