wp query - Displaying pages on an archive page

admin2025-06-02  2

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>

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>
Share Improve this question asked Feb 25, 2019 at 20:02 user3205234user3205234 931 gold badge2 silver badges6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748867442a314382.html

最新回复(0)