2013-10-14 187 views
1

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

但是,您可以从标题中看到并阅读,当帖子被选为顶部精选空间中的地点时,它也会显示在最新的帖子Feed中。

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

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

希望您能抽出时间帮我:)

回答

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

+0

谢谢,但如何确保将最新的帖子循环总是会检查$ top_post_id总是包含某一类最新帖子的ID? – Lund

+0

我认为你的最新帖子循环已经可以工作 - 如果是这种情况,那么你不必 - 只需将get_the_id()或$ post-> ID返回的值保存在输出的精选帖子循环中顶部帖子 - 这是您要输出的帖子的ID,以及您希望稍后排除的帖子。基本上,当您在顶部输出帖子时,保存它的ID,然后再排除此ID。 –

+0

问题是,使用(get_template_part('includes/feat-slider'))从不同的模板中获取特色的帖子。因此,特色的帖子循环与最新的帖子循环不在同一个文件中。所以,如果有办法总是获得某个类别中最新帖子的ID。 – Lund

0

启动一个变量,并检查您的循环中。一个简单的方法:

$i=0; 

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

// Rest of the code 
} 
0

的,你可以用

query_posts('offset=1'); 

更多信息:blog

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篇文章。这个代码中的重要部分是“抵消”和这个魔术词正在做整件事情。

更多细节from Here

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; 

}

用法:说我的类别编号为22,则:

$last_post_ID = get_lastest_post_of_category(22); 

你也可以传递一个类别数组到这个函数。

0

排除第一个从最新的五个职位

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