3

I have a single-news page in WordPress where I am looping over all the posts and conditionally updating the previous and next buttons to exclude certain categories based on the current post's category ID.

Here is what I have:

          <?php if (have_posts()): ?>
            <?php while (have_posts()): ?>
                <?php the_post(); ?>

                <?php if ( in_category(7)) : ?>

                    <?php
                        $sidebar = 'blog-news';        
                        $catagory = array(3,5,6,4,1);
                    ?>
                <?php endif; ?>

        <nav>
            <ul class="pager">
                <li class="prev">
                    <?php 
                        echo previous_post_link( "%link", "Previous", true, $catagory );
                    ?>
                </li>
                <li class="next">
                    <?php 
                        echo next_post_link( "%link", "Next", true, $catagory );
                    ?>
                </li>
            </ul>
        </nav>
    <?php endwhile; ?>
    <?php endif; ?>

The problem I'm having is that if a post has 2 categories e.g. 6 and 7, it excludes that post from the link, whereas if the post has only one category e.g. 6 then it does not exclude it.

How do I set it so that it DOES NOT exclude any post that has category 7, regardless of if it has multiple categories.

  • So what you want to achieve is not to exclude posts with category ID = 6, correct? If so, why do you have that particular ID number in your $catagory array? Correct me if I'm wrong, it could be that I misunderstood your problem and/or given example. – wscourge Nov 7 '16 at 12:46
  • Oops, my mistake, I meant to say "not to exclude any post that has category 7". Edit made. – user3574492 Nov 21 '16 at 13:30
0

What you need to do is change the excluded_terms argument of the previous_post_link and next_post_link functions which you called $catagory. Simply remove all the category ids of your current post from that excluded_terms array. Make sure to define an array of categories which you don't want to be displayed like you did it in your example above.

$catagory = array(3,5,6,4,1) // array of category ids you don't want to be displayed

the next step is to find all the category ids of your current post and put it in a new array.

$post_category_objects = get_the_category(); // returns an array of WP_Term objects
$post_category_ids = array();
foreach($post_category_objects as $pco){
     array_push($post_category_ids, $pco->term_id); // adds the post's category id to the $post_category_ids array
}

next you need to remove the category ids of the current post from $catagory (<-- the array which contains the values to be excluded from the previous_post_link and next_post_link functions).

Assuming the post has the categories 6 and 7, these category ids will be removed from $catagory.

// removes the current post's category ids from the $catagory-array
   foreach( $post_category_ids as $pci ){
        $key = array_search( $pci, $catagory );
        if( $key !== false ){
             unset( $catagory[$key]);
        }
   }
// rearranges the $catagory's keys
   $catagory = array_values($catagory);

Put the whole code above in your if-statement and it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.