2009-12-30 77 views
0

下面的代碼顯示了一個WordPress的循環。我如何修改此循環以在頁面上僅顯示一個帖子?哪裏可以查詢只顯示Wordpress中的一個帖子

<?php if (have_posts()) : ?> 
<?php while (have_posts()) : the_post(); ?> 
<div class="fullbox" id="post-<?php the_ID(); ?>"> 
<h3><?php the_category(', ') ?></h3> 
<div class="fullbox_content"> 
<h1><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> 
+0

這個問題對我來說並不合適。嘗試重新措辭,我會盡力幫助。 – 2009-12-30 18:05:55

+0

現在它更有意義嗎? – drew 2009-12-30 18:11:04

回答

1
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

<?php static $count = 0; 
if ($count == "n") { break; } 
else { ?> 

<div class="fullbox" id="post-<?php the_ID(); ?>"> 
<h3><?php the_category(', ') ?></h3> 
<div class="fullbox_content"> 
<h1><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> 

<?php $count++; } ?> 
<?php endwhile; ?> 
<?php endif; ?> 
你的情況

所以, 「N」 是1

這裏找到:http://perishablepress.com/press/2007/08/06/super-loop-exclude-specific-categories-and-display-any-number-of-posts/

+0

太棒了,謝謝。我得到錯誤分析錯誤:語法錯誤,意外T_ENDWHILE – drew 2009-12-30 18:59:27

+0

這似乎仍然是「最先進的」解決方案。我不能相信它,全世界最常用的CMS不能以更清潔的方式做到這一點!^^ – davidb 2011-12-05 09:10:50

0

感謝Hatdrawn。你指出我的方向正確,我可以使用你給我的參考來修改代碼。最後編輯的循環是 `

<?php static $count = 0; 
if ($count == "1") { break; } 
else { ?> 

    <div class="fullbox" id="post-<?php the_ID(); ?>"> 
    <h3> 
    <?php the_category(', ') ?> 
    </h3> 
    <div class="fullbox_content"> 
    <h1><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"> 
    <?php the_title(); ?> 
    </a></h1> 

,並與另外的

<?php $count++; } ?> 
<?php endwhile; ?> 

我現在已經在主頁上只有一個職位顯示結束。甜!!!

0

通過使用插件來實現此目的的另一種方法是獲得一個插件,如Recent Post,Nick Momrick。它允許您使用自定義模板標籤來完成相同的事情。我在我的網站上使用這個插件的修改版本。尼克在同一條線上有很多有用的插件http://nickmomrik.com/code/.

希望這有幫助。

相關問題