在分页页面中显示不需要的重复帖子

时间:2020-04-17 作者:jpollar

出于某种原因,我有一些帖子使用下面的代码在某些分页页面上不断重复。这完全是随机的,所以我不知道如何解决这个问题。也许是帖子本身的某些东西导致了它的重复?

以下是一些问题示例:

<?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; ?>

1 个回复
最合适的回答,由SO网友:Himad 整理而成

尝试使用自定义WP\\U查询,以排除任何可能的代码干扰主查询分页。像这样:

<?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>

相关推荐

get_posts returns all posts

我很难弄明白get_posts 函数返回所有帖子,而不是特定帖子。我有一个帖子类型叫做mall, 其中一种分类法称为shop 有两个术语Compi 和Macho. 在变量中$store_name 当我在中使用变量时,会存储其中一个术语(取决于订单中的产品)terms 对于$malls_args, 我要买所有的购物中心。即使没有存储在变量中的术语$store_name.我尝试硬编码变量并使用\"Copmi\" 内部terms 但我还是能拿到所有的购物中心。add_action( \'woocommerce_