0

So im making a custom WP_query loop with paged navigation. I want to show 4 posts per page in a specific category.

This is working fine, except the next_post_link() seems to still count the excluded catoegories when determining to show the link or not. How do i make sure that the post count is excluding the others categories, so that the next link only shows up if there is more then the 4 shown posts in that specific category?.

my code:

<div class="news-box-wrapper">
        <?php 

            $args = array ('post_type' => 'post','category__in' => array( 1 ), 'posts_per_page'=> 4, 'paged' => get_query_var('paged'),);
            $posts_query = new WP_query( $args );
        ?>


        <?php //Starts the news loop ?>
        <?php if ( $posts_query->have_posts() ) : while ( $posts_query->have_posts() ) : $posts_query->the_post(); ?>

            <article class="news-box">
                <?php //get the posts featured image in the right size = "featured-image" ?>
                <?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'newsbox-image'); ?>
                <img src="<?php echo $image[0]; ?>" alt="Post image" width="700" height="400" />

                <div class="news-box-content">    
                    <h2><?php the_title(); ?></h2>
                    <p class="news-date"><?php the_time('j F, Y'); ?></p>
                </div>

                <a class="read-more" href="<?php the_permalink() ?>"></a>
            </article>
        <?php endwhile; else: endif;?>

    </div>

    <nav class="articles-nav">
        <?php previous_posts_link('Nyere'); ?><?php next_posts_link('Ældre'); ?>
    </nav>

    <?php wp_reset_postdata(); ?>

Any help in understanding this is much appreciated

1
1

Okay found a solution as soon as i posted this. Typical google-syndrome huh? - i had to pass the WP_query variable to the max_num_pages var in my next_posts_link, so it looks like this:

<?php next_posts_link('Ældre', $posts_query->max_num_pages); ?>

guess same thing goes for the previous_posts_link

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.