admin管理员组

文章数量:1290994

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');

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');
Share Improve this question edited Jun 15, 2021 at 12:46 jonzal asked Jun 14, 2021 at 21:17 jonzaljonzal 11 bronze badge 1
  • Still stumped here. Is there some way to call a previous_post_link and next_post_link from category 2 specifically as opposed to trying to exclude categories...? – jonzal Commented Jun 19, 2021 at 21:12
Add a comment  | 

1 Answer 1

Reset to default 0

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 ;)

本文标签: nextprevious post links for specific category when post belongs to multiple categories