admin管理员组

文章数量:1386706

For some reason, I have posts that keep repeating on certain pagination pages using the code below. It's completely random, so I have no idea how to fix the problem. Maybe it's something in the post itself that's causing it to be repeated?

Some examples of the problem can be found here:

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

<div class="blogcontent"> 
    <div class="post_img"><a href="<?php the_permalink(); ?>">
    <?php $img = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
    <?php $image_alt = get_post_meta( get_post_thumbnail_id($post->ID), '_wp_attachment_image_alt', true); ?>
                <?php if($img) { ?>
                    <img src="<?php echo $img; ?>" alt="<?php echo $image_alt; ?>">
                <?php } ?></a></div>
    <div class="post_content">
    <?php 
        $category_detail=get_the_category($post->ID);//
            foreach($category_detail as $cd){ ?>

                <a href="<?php echo get_home_url(); ?>/category/<?php echo $cd->slug; ?>/"><span class="boldtag"><?php echo $cd->cat_name; ?></span></a>
        <?php       }
    ?>

        <h1><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h1>
        <?php the_excerpt(); ?>
        <ul class="author clearfix">
            <li>By <em><?php the_author_posts_link(); ?></em> <span>· <?php the_time('M d, Y') ?></span></li>
            <li><a href="<?php the_permalink(); ?>">MORE...</a></li>
        </ul>
    </div>
</div>
<?php endwhile; ?>

<div class="paginationsec">
    <?php // Previous/next page navigation.
        the_posts_pagination( array(
            'prev_text'          => __( '<i class="fa fa-angle-double-left"></i> Prev', 'twentyfifteen' ),
            'next_text'          => __( 'Next <i class="fa fa-angle-double-right"></i>', 'twentyfifteen' ),
            'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( '', 'twentyfifteen' ) . ' </span>',
        ) ); ?>

</div>

<?php else: endif; ?>

For some reason, I have posts that keep repeating on certain pagination pages using the code below. It's completely random, so I have no idea how to fix the problem. Maybe it's something in the post itself that's causing it to be repeated?

Some examples of the problem can be found here:

  • https://www.suddenlysenior/page/64/
  • https://www.suddenlysenior/page/65/
  • https://www.suddenlysenior/page/66/
  • https://www.suddenlysenior/page/67/
  • https://www.suddenlysenior/page/68/
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>   

<div class="blogcontent"> 
    <div class="post_img"><a href="<?php the_permalink(); ?>">
    <?php $img = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
    <?php $image_alt = get_post_meta( get_post_thumbnail_id($post->ID), '_wp_attachment_image_alt', true); ?>
                <?php if($img) { ?>
                    <img src="<?php echo $img; ?>" alt="<?php echo $image_alt; ?>">
                <?php } ?></a></div>
    <div class="post_content">
    <?php 
        $category_detail=get_the_category($post->ID);//
            foreach($category_detail as $cd){ ?>

                <a href="<?php echo get_home_url(); ?>/category/<?php echo $cd->slug; ?>/"><span class="boldtag"><?php echo $cd->cat_name; ?></span></a>
        <?php       }
    ?>

        <h1><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h1>
        <?php the_excerpt(); ?>
        <ul class="author clearfix">
            <li>By <em><?php the_author_posts_link(); ?></em> <span>· <?php the_time('M d, Y') ?></span></li>
            <li><a href="<?php the_permalink(); ?>">MORE...</a></li>
        </ul>
    </div>
</div>
<?php endwhile; ?>

<div class="paginationsec">
    <?php // Previous/next page navigation.
        the_posts_pagination( array(
            'prev_text'          => __( '<i class="fa fa-angle-double-left"></i> Prev', 'twentyfifteen' ),
            'next_text'          => __( 'Next <i class="fa fa-angle-double-right"></i>', 'twentyfifteen' ),
            'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( '', 'twentyfifteen' ) . ' </span>',
        ) ); ?>

</div>

<?php else: endif; ?>

Share Improve this question asked Apr 17, 2020 at 17:22 jpollarjpollar 1310 bronze badges 10
  • No need to use "if(have_posts()) " – Ali Ali Commented Apr 21, 2020 at 18:03
  • Did you try disabling all plugins to see if the problem persists? When did you start noticing the duplicate posts? – Himad Commented Apr 21, 2020 at 18:27
  • It's persisted before plugins were even installed. The original developer couldn't figure it out either. – jpollar Commented Apr 21, 2020 at 18:28
  • If you run a WP_Query() and print its results, do you get duplicate posts? – Himad Commented Apr 21, 2020 at 18:41
  • 1 I figured out what's causing the problem, I just don't understand why. I have a bunch of old posts that were published on the exact date and time on March 26, 2018. When I change the publish date for posts that repeat, it no longer repeats it in the navigation. Why would a post published date-timestamp cause it to repeat in navigation???? – jpollar Commented Apr 21, 2020 at 20:23
 |  Show 5 more comments

1 Answer 1

Reset to default 4 +100

Try with a custom WP_Query to rule out any possible code messing up with your main query pagination. Like this:

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 5, 'paged'=> $paged) );

?>
<?php while($query->have_posts()) : $query->the_post(); ?>   

<div class="blogcontent"> 
    <div class="post_img"><a href="<?php the_permalink(); ?>">
    <?php $img = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
    <?php $image_alt = get_post_meta( get_post_thumbnail_id($post->ID), '_wp_attachment_image_alt', true); ?>
                <?php if($img) { ?>
                    <img src="<?php echo $img; ?>" alt="<?php echo $image_alt; ?>">
                <?php } ?></a></div>
    <div class="post_content">
    <?php 
        $category_detail=get_the_category($post->ID);//
            foreach($category_detail as $cd){ ?>

                <a href="<?php echo get_home_url(); ?>/category/<?php echo $cd->slug; ?>/"><span class="boldtag"><?php echo $cd->cat_name; ?></span></a>
        <?php       }
    ?>

        <h1><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h1>
        <?php the_excerpt(); ?>
        <ul class="author clearfix">
            <li>By <em><?php the_author_posts_link(); ?></em> <span>· <?php the_time('M d, Y') ?></span></li>
            <li><a href="<?php the_permalink(); ?>">MORE...</a></li>
        </ul>
    </div>
</div>
<?php endwhile; ?>
<div class="paginationsec">
  <nav class="navigation pagination show">
    <div class="nav-links">
    <?php 
        echo paginate_links( array(
            'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
            'total'        => $query->max_num_pages,
            'current'      => $paged,
            'format'       => '?page=%#%',
            'show_all'     => false,
            'type'         => 'plain',
            'end_size'     => 5,
            'mid_size'     => 1,
            'prev_next'    => true,
            'prev_text'    =>__( '<i class="fa fa-angle-double-left"></i> Prev', 'twentyfifteen' ),
            'next_text'    => __( 'Next <i class="fa fa-angle-double-right"></i>', 'twentyfifteen' ),
            'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( '', 'twentyfifteen' ) . ' </span>',
            'add_args'     => false,
            'add_fragment' => '',
        ) );
    ?>
    </div>
  </nav>

</div>

本文标签: Unwanted Duplicate Posts Showing in Pagination Pages