2013-05-14 82 views
1

在首頁和博客頁面 - 側邊欄顯示最近的帖子,我發現它看起來不是很好,與主頁上展開的同一帖子重複。如何從WordPress中的側邊欄刪除最新的帖子?

這是我的側邊欄代碼:

<div class="blog-sidebar"> 
<?php query_posts('showposts=5'); ?> 
<?php while (have_posts()) : the_post(); ?> 
    <div class="blog-sidebar-feature"> 
     <?php if (has_post_thumbnail()) { ?> 
      <div class="blog-sidebar-image"><a href="<?php the_permalink() ?>" rel="bookmark" title=""><?php the_post_thumbnail('medium'); ?></a></div> 
     <?php 
     } 
     ?> 
     <div class="blog-sidebar-content"> 
      <p class="date"><?php the_time('F j, Y') ?></p> 
      <h3 <strong><?php 

    foreach((get_the_category()) as $category) { 
echo $category->cat_name . ' '; 
    } 
    ?></strong></h3> 
    <h2 <p><a href="<?php the_permalink() ?>" rel="bookmark" title=""><?php the_title();   
    ?></a></p></h2><?php echo get_excerpt(166); ?> 
     </div> 
    </div> 
<?php endwhile;?> 
<br /> 
<?php wp_pagenavi(); ?> 
</div> 

,並在博客如何顯示在主頁上的相關代碼:

<div class="blog-sidebar"> 
    <div class="blog-sidebar-feature"> 
     <?php query_posts('orderby=date&order=DESC&showposts=2'); ?> 
      <?php while (have_posts()) : the_post(); ?> 
      <?php if (has_post_thumbnail()) { ?> 
       <div class="blog-sidebar-image"><a href="<?php the_permalink() ?>" rel="bookmark" title=""><?php the_post_thumbnail('medium'); ?></a></div> 
      <?php 
      } 
      ?> 
      <div class="blog-sidebar-content"> 
       <p class="date"><?php the_time('F j, Y') ?></p> 
      <h3 <strong><?php 
    foreach((get_the_category()) as $category) { 
echo $category->cat_name . ' '; 
    } 
    ?></strong></h3> 
       <h2 <p><a href="<?php the_permalink() ?>"  
    rel="bookmark" title=""><?php the_title(); ?></a></p></h2><?php echo get_excerpt(166); ?> 
      </div> 
     <?php endwhile;?> 
    </div> 

</div> 
<div id="connect"> 
    <?php query_posts('page_id=1'); 
     while (have_posts()): the_post(); 
     the_content(); 
    endwhile; 
    wp_reset_query(); ?> 
</div> 

有什麼辦法,只除去最近當它出現在主容器上時,從側邊欄發佈?預先感謝您的幫助。

回答

1

UPDATE V2

所以,你想最近的帖子,只是沒有後目前顯示的主要內容。

UPDATE V3:

這應該現在的工作。我不得不將query_posts的參數改爲數組,以使其工作。

現在就來試試:

<? 
global $wp_query; 
$skip_posts=array(); 
if (is_single()) //only exclude posts when single post is shown 
$skip_posts[]=$wp_query->post->ID; 
?> 
<?php query_posts(array('showposts'=>5,'post__not_in'=>$skip_posts)); ?> 
+0

我認爲上面的代碼是在sidebar.php。它在那裏還是在別的地方? – user850010 2013-05-15 07:17:44

+0

是的,它在sidebar.php中。我在sidebar.php和home-template.php(其中一個精選的最新博客文章和邊欄文章也出現)中嘗試了您的建議,但不幸的是它使邊欄消失在兩者中。 – Reibusu 2013-05-15 09:32:01

+0

嘗試更換if語句 - if(!is_front_page()&&!is_home()) – user850010 2013-05-15 09:40:02

1
<?php query_posts('posts_per_page=5&offset=1'); ?> 

感謝850010所有幫助,我回去看了一下偏移規則和「陣列」是沒有必要的。看似簡單。

+0

該代碼跳過最新的帖子,並沒有真正解決您的重複問題,當你去任何舊帖子,從 http://av5theapiaryconz.avatar.net.nz/blog/upcycle/hexagonal-honeyjar-spice-rack/ - 檢查我更新的v3代碼 – user850010 2013-05-15 19:12:09

相關問題