next/previous post links for specific category when post belongs to multiple categories

I have a category called Featured Photo that is category ID 2. I want to create next-post and previous-post links that contain links only for the next and previous posts in category ID 2. However, many Featured Photo posts also belong to more than one category, and I have so far been unable to find a solution that limits the Next and Previous links to category 2. If I do not exclude any categories in the previous_post_link() and next_post_link() calls, those links are populated with the URL to the previous/next post in one of the other multiple categories assigned to the Featured Photo posts. Thus, I created a variable containing an array of all of the other category IDs and added it to the exclude portion of the previous_post_link() and next_post_link() calls, but that is just resulting in the links not showing up at all. (I've searched extensively for a solution, and haven't found anything that works.)

function featured_photo() {
        global $post;
        if (is_single()  in_category(2)) {
        $category = array(1,3,4,5,6,7,8,9,67,68,69,74,76,78,82);
        $pod = get_post_meta($post-ID, 'pod', true);
        echo 'div id=pod';
        echo the_title( 'h1 class=entry-title', '/h1' );
        echo $pod;
        previous_post_link( '%link', 'Previous photo', true, $category );
        next_post_link( '%link', 'Next photo', true, $category );
        echo '/div';
        }
}
add_action( 'genesis_before_content','featured_photo');

Topic next-post-link previous-post-link Wordpress

Category Web


did you search around the functions get_adjacent_post() ?

A long time ago, I made a Woocommerce plugin (an old outdated Woocommerce product navigation on the repo) that use it in the same case and it used to work.

The class I created, use a fallback function to get all ids to exclude.

Hope it helps ;)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.