Site logo

Archived topic

remove category with function php tom

3 replies · Started by alexis on May 11, 2020

Viewing posts 1–4 of 4

I found this code that I customized (@tom unsborne) as I wanted. How to include category exclusion. I only want the category related to the article in the next and previous article function. If possible have something dynamic. Examples of articles from such categories I find only articles from the same category. A snippet for the whole site. If this is possible please

<div id="post-nav">
    <ul class="flex">
	<?php global $post;
    $prevPost = get_previous_post(false);
    $post_type = get_post_type();
    
        if($prevPost) {
            $args = array(
                'posts_per_page' => 1,
                'include' => $prevPost->ID,
                'post_type' => $post_type,
            );
            $prevPost = get_posts($args);
            foreach ($prevPost as $post) {
                setup_postdata($post);
    ?>
        <li>
        <a class="post-previous" href="<?php the_permalink(); ?>" rel="prev">
        <strong><?php the_title(); ?></strong><span>Mariage précédent</span>
        </a>
        </li>
    <?php
                wp_reset_postdata();
            } //end foreach
        } // end if
         
        $nextPost = get_next_post(false);
        if($nextPost) {
            $args = array(
                'posts_per_page' => 1,
                'include' => $nextPost->ID,
                'post_type' => $post_type,
            );
            $nextPost = get_posts($args);
            foreach ($nextPost as $post) {
                setup_postdata($post);
    ?>
        <li>
        <a class="post-next" href="<?php the_permalink(); ?>" rel="next">
        <strong><?php the_title(); ?></strong><span>Mariage suivant</span>
        </a>
        </li>

    <?php
                wp_reset_postdata();
            } //end foreach
        } // end if
    ?>
    </ul>
</div>

Hi there,

change the false to true in these two lines:

$prevPost = get_previous_post(false);

$nextPost = get_next_post(false);

Perfect! Thank you David !

You're welcome

This archived topic is closed to new replies.