仅从最新的帖子Feed,wordpress中排除一个类别的第一篇帖子

时间:2013-10-14 21:38:22

标签: php wordpress content-management-system article featured

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

但是,正如您可以看到并阅读标题一样,当帖子被选为顶部特色空间中的位置时,它也会显示在最新的帖子中。

我的问题是我的标题:我如何排除特定类别中的最新/最新帖子与所有最新帖子一起出现。

我知道我可以通过一段时间之后改变类别来手动控制它,但我想让它自动完成,我不知道如何。

希望你能节省一些时间并帮助我:)

6 个答案:

答案 0 :(得分:3)

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

如果没有看到你的模板代码,很难具体,但这样的事情可能会有效:

在顶部,保存您要输出的帖子的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;

然后,在主循环中,改变the query以排除该帖子:

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;

更多信息hereherehere

答案 1 :(得分: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);

你也可以将一系列类别传递给这个函数。

答案 2 :(得分:0)

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

$i=0;

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

 // Rest of the code
}

答案 3 :(得分:0)

为此你可以使用

query_posts('offset=1');

了解更多信息:blog

答案 4 :(得分:0)

方法 - 1

$cat_posts = new WP_Query('posts_per_page=1&cat=2'); //first 1 posts
while($cat_posts->have_posts()) { 
   $cat_posts->the_post(); 
   $do_not_duplicate[] = $post->ID;
}

//Then check this if exist in an array before display the posts as following.
 if (have_posts()) {
    while (have_posts()) {

    if (in_array($post->ID, $do_not_duplicate)) continue; // check if exist first post

     the_post_thumbnail('medium-thumb'); 

         the_title();

    } // end while
}

方法 - 2

query_posts('posts_per_page=6&offset=1');
if ( have_posts() ) : while ( have_posts() ) : the_post();

此查询告诉循环仅显示最近的第一篇帖子后面的5个帖子。这段代码中的重要部分是“offset”,这个神奇的词语正在做整件事。

更多详情from Here

答案 5 :(得分:0)

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

<?php 
   // the query
   $the_query = new WP_Query( array(
     'category_name' => 'Past_Category_Name',
      'posts_per_page' => 5,
              'offset' => 1
   )); 
?>