0

I'm trying to display a list of pages on an archive page. The query I've written is meant to only fetch pages but for some reason it is also displaying one post (from the archive page this is on) amongst the pages.

Can anyone suggest what the issue is?

Thanks in advance.

    <?php $page_args = array(
        'post_type' => 'page'
    ); ?>

    <?php $page_query = new WP_Query($page_args); ?>

    <div class="links_slider">
        <a class="slide" href="<?php the_permalink(); ?>">
            <?php the_post_thumbnail('links'); ?>
            <span><?php the_title(); ?></span>
        </a>
        <?php while($page_query->have_posts()):$page_query->the_post(); ?>

            <a class="slide" href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail('links'); ?>
                <span><?php the_title(); ?></span>
            </a>
        <?php endwhile; ?>

    </div>

2 Answers 2

1

The issue turned out to be in another part of my code and nothing to do the the above query, which works just fine.

1
  • Then you should mark your this answer as accepted so the other user know about it. Thanks. Feb 26, 2019 at 10:20
0

Apparently this part of code before while loop is introducing undesired result.

    <a class="slide" href="<?php the_permalink(); ?>">
        <?php the_post_thumbnail('links'); ?>
        <span><?php the_title(); ?></span>
    </a>

So the revised code should be something:

<?php $page_args = array(
        'post_type' => 'page'
    ); ?>

    <?php $page_query = new WP_Query($page_args); ?>

    <div class="links_slider">
        <?php while( $page_query->have_posts() ):  $page_query->the_post(); ?>

            <a class="slide" href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail('links'); ?>
                <span><?php the_title(); ?></span>
            </a>

        <?php endwhile; ?>

        <?php wp_reset_postdata(); ?>

    </div>
2
  • Thanks for your help, unless I've missed something it looks like you added wp_reset_postdata at the end, which I have in my code (apologies for not including it here, I will update my question). Feb 25, 2019 at 21:54
  • Yes and also removed a part of your original code. Feb 26, 2019 at 10:21

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.