在我的wordpress主题中,我想在页脚中添加一个侧循环来获取最新的帖子。 此循环的第一个帖子显示拇指像素,标题和帖子预览.. 以下5只显示标题/链接。
因为我已经使用了常规<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
在主div中,我必须使用基于get_posts()
这是我想要的,但它不起作用:
<?php query_posts('cat=6&showposts=5'); ?>
<?php $posts = get_posts('category=6&numberposts=5');
$count = count($posts);
foreach ($posts as $post) : start_wp(); ?>
<?php if ($count < 2) : ?>
/// code for the 1st post (thumb etc..)
<?php else : ?>
/// code for the 4 following post (links to posts only)
<?php endif; ?>
<?php endforeach; ?>
我知道如何为常规wordpress循环添加计数/条件,但不知道如何为get_posts()函数添加计数/条件。
你能帮助我实现这个目标吗?
提前致谢;)
编辑:解决方案:
好的,我使用'offset'参数来实现:
<?php $posts = get_posts('numberposts=2&offset=0');
foreach ($posts as $post) : start_wp(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" class="footernews-thumb">
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail(thumbnail); ?>
<?php endif; ?>
</a>
<h2 class="footernews-title"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<p class="footernews-preview"><?php the_content_rss('', TRUE, '', 20); ?></p>
<?php endforeach; ?>
<?php $posts = get_posts('numberposts=3&offset=1');
foreach ($posts as $post) : start_wp(); ?>
<h2 class="footernews-title"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php endforeach; ?>
由于“循环”实际上不是循环,我决定避免计算出现次数。
答案 0 :(得分:0)
您将$count
设置为您获得的帖子数量(5),因此不会小于2。
您需要将其更改为:
<?php query_posts('cat=6&showposts=5'); ?>
<?php $posts = get_posts('category=6&numberposts=5');
$first = true;
foreach ($posts as $post) : start_wp(); ?>
<?php if ($first) : ?>
/// code for the 1st post (thumb etc..)
<?php $first = false; ?>
<?php else : ?>
/// code for the 4 following post (links to posts only)
<?php endif;?>
<?php endforeach; ?>
答案 1 :(得分:0)
我会使用简单的递增检查......
<?php query_posts('cat=6&showposts=5'); ?>
<?php $posts = get_posts('category=6&numberposts=5');
$i = 1;
foreach ($posts as $post) : start_wp(); ?>
<?php if ($i == 1) : ?>
/// code for the 1st post (thumb etc..)
<?php else : ?>
/// code for the 4 following post (links to posts only)
<?php endif; ?>
<?php ++$i;
<?php endforeach; ?>
<?php unset($i); ?>
这将只为第一次迭代提供额外的输出,之后它将继续使用较小的代码。