1

我管理一个运行 Wordpress的网站 ( www.teknologia.no )。正如您在首页上看到的那样,我在页面顶部有一篇“主要/精选”文章,显示了来自特定类别的最新帖子。在它下面,我有一个主循环,显示所有类别的所有最新帖子。

但正如您从标题中看到和读到的那样,当一个帖子被选择放在顶部的特色空间中时,它也会显示在最新的帖子提要中。

我的问题正如我的标题所说:如何将某个类别中的最新/最新帖子排除在所有最新帖子中。

我知道我可以通过在一段时间后更改类别来手动控制它,但我希望它自动完成,我不知道如何。

希望你能抽出一些时间来帮助我:)

4

6 回答 6

4

您需要更新模板的逻辑,以便主循环跳过输出顶部输出的帖子。

如果没有看到您的模板代码,很难具体说明,但这样的事情可能会起作用:

在顶部,保存您要输出的帖子的 ID:

$exclude_post_id = get_the_ID();

如果您需要直接获取给定类别中最新帖子的 ID,而不是在循环期间保存它,您可以这样做,使用WP_Query

$my_query = new WP_Query('category_name=my_category_name&showposts=1');
while ($my_query->have_posts()):
    $my_query->next_post();
    $exclude_post_id = $my_query->post->ID;
endwhile;

然后,在主循环中,更改查询以排除该帖子:

query_posts(array('post__not_in'=>$exclude_post_id));

或手动将其排除在循环内,如下所示:

if (have_posts()): 
    while (have_posts()):
        the_post();
        if ($post->ID == $exclude_post_id) continue;
        the_content();
    endwhile;
 endif;

更多信息在这里这里这里

于 2013-10-14T21:58:41.853 回答
1

这是一个可以做到这一点的函数:

function get_lastest_post_of_category($cat){
$args = array( 'posts_per_page' => 1, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => (array)$cat);
$post_is = get_posts( $args );
return $post_is[0]->ID;

}

用法:假设我的类别 id 是 22,那么:

$last_post_ID = get_lastest_post_of_category(22);

您还可以将类别数组传递给此函数。

于 2015-03-20T16:15:52.973 回答
0

启动一个变量并检查循环内部。一个简单的方法:

$i=0;

while(have_posts() == true)
{
 ++$i;
 if($i==1) //first post
  continue;

 // Rest of the code
}
于 2013-10-14T21:44:17.687 回答
0

为此,您可以使用

query_posts('offset=1');

欲了解更多信息:博客

于 2013-10-15T05:10:55.770 回答
0
于 2014-01-15T07:13:11.087 回答
0

从最近的五个帖子中排除第一个

<?php 
   // the query
   $the_query = new WP_Query( array(
     'category_name' => 'Past_Category_Name',
      'posts_per_page' => 5,
              'offset' => 1
   )); 
?>
于 2018-01-12T11:51:00.333 回答