2013-10-14 103 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 
    )); 
?> 
相關問題