wordpress firstpost类别
问题描述:
嗨即时尝试从类别5获得第一篇文章,并作出两个不同类。但是我所有的帖子都是“正规”的课程,我想要第一篇文章(只有第一篇文章)让课程“有特色”,而其余的课程“常规”不能弄清楚什么是错的。 HALP! :) 哦,顺便说一句代码工作,如果我删除代码的第一线,但随后得到的所有帖子所有类别:/wordpress firstpost类别
<?php query_posts('cat=5');
while (have_posts()) : the_post(); ?>
<?php if (is_paged()) : $postclass = ('regular'); ?>
<?php else : ?>
<?php $postclass = ($post == $posts[0]) ? 'featured' : 'regular'; ?>
<?php echo $postclass; ?>
<?php endif; ?>
<?php endwhile ?>
答
为什么不尝试设置$postclass
为“精选”你的循环之外,并一旦显示,将其设置为“常规”?
使不改变逻辑的微小变化对你的代码:
<?php
query_posts('cat=5');
$postclass = 'featured';
while (have_posts()) : the_post();
if (is_paged()) :
$postclass = 'regular';
else :
echo $postclass;
$postclass = 'regular';
endif;
endwhile
?>
或者如果没有对你有意义,有关使用布尔怎么样?
<?php
query_posts('cat=5');
$first = true;
while (have_posts()) : the_post();
if (is_paged()) :
$postclass = 'regular';
else :
$postclass = $first ? 'featured' : 'regular';
echo $postclass;
$first = false;
endif;
endwhile
?>