2009-12-30 80 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/.

希望这有帮助。

相关问题