【问题标题】:WP_Query duplicate postWP_Query 重复帖子
【发布时间】:2017-07-03 03:36:41
【问题描述】:

我在我的 wordpress 主题中使用了 wp_query。 我有重复的帖子我的主页。 这要怎么修?这个问题跟什么有关系?

整个悖论是,如果您将此代码用作单独的模板,那么一切正常。如果您将主题嵌入到模块中,那么所有帖子都会按顺序排列。然后是 (1,2,3,4,5,1,2,3,4,5)。

谢谢。

}

function render() {
    ob_start();
    ?>               
           <?php $custom_query = new WP_Query('posts_per_page= 1');
             while($custom_query->have_posts()) : $custom_query->the_post();
                    ?>
              <div class="post-1">
                <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">          
                  <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </h3>    
                     <div class="float-tags">          
                                    <div class="bb-td-post-small-box clearfix">        
                                      <?php the_tags('','',''); ?>
                                    </div>
                     </div>
                  <div class="post-2">                                            
                    <?php the_content(); ?>    
                  </div>

                </div>
              </div>  

             <?php endwhile; ?>  
             <?php wp_reset_postdata(); // reset the query ?>


    <?php return ob_get_clean();
}
}

【问题讨论】:

  • 所以你的数据库中有重复的数据?
  • 复制我的主页,看截图
  • 能否请您发送您的管理仪表板 [帖子列表和页面列表] 部分的屏幕截图。

标签: wordpress


【解决方案1】:
<?php

function render($offset,$posts_per_page) 
{
    ob_start();
    $args['offset']=$offset;
    $args['posts_per_page']= $posts_per_page;
    $custom_query = new WP_Query($args);
    while($custom_query->have_posts()) : $custom_query->the_post();

?>
              <div class="post-1">
                <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">          
                  <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </h3>    
                     <div class="float-tags">          
                                    <div class="bb-td-post-small-box clearfix">        
                                      <?php the_tags('','',''); ?>
                                    </div>
                     </div>
                  <div class="post-2">                                            
                    <?php the_content(); ?>    
                  </div>

                </div>
              </div>  

             <?php endwhile; ?>  
             <?php wp_reset_postdata(); // reset the query ?>


    <?php return ob_get_clean();
}
render(0,5); //shows first 5,  0 to 5
render(5,5); //shows second 5,  5 to 10
render(5,5); //shows third 5,  10 to 15

【讨论】:

  • 空白页,只有页眉和页脚。整个悖论是,如果您将此代码用作单独的模板,那么一切正常。如果您将主题嵌入到模块中,那么所有帖子都会按顺序排列。然后又是 (1,2,3,4,5,1,2,3,4,5)。
  • @Ivan 很抱歉我无法理解这条评论。
  • 如果您使用此代码,则页面上没有帖子。
  • 只需删除行“ob_start();”和“return ob_get_clean();”你会得到结果。我刚刚测试了代码,如果被删除,我可以工作。
  • 你可以说你的邮件,我给你写登录并通过。
【解决方案2】:

通过这种方式,您将始终获得一篇文章,该文章将重复您在代码中调用 render 函数的次数。如果您希望每次都显示不同的帖子,那么您必须在 WP_QUERY 中指定帖子的偏移量。 编号:https://codex.wordpress.org/Class_Reference/WP_Query [偏移量] 注意:像 PHP 中的限制一样。

希望对你有所帮助。

【讨论】:

  • 能详细点吗?我不太明白。
  • 如果我执行 'posts_per_page= 5' ,则显示 5 个帖子,然后再次显示 5 个帖子。
  • 看,你需要给下一次的开始索引,它将以0作为开始索引,显示从0开始的5个帖子。下次你需要改变它,比如 0+5=5 之类的
  • 查看下一个答案
猜你喜欢
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 2021-07-01
  • 1970-01-01
  • 2013-01-13
  • 1970-01-01
相关资源
最近更新 更多