我写了一些代码来显示来自特定类别的最新5个帖子,但是我不知道如何让它显示该类别中标记为仅有特色的最新5个帖子。我的意思是这个帖子已经被贴上了,所以基本上它会显示每个类别中被贴上的5个帖子。
<ul id="index-blog">
<?php $the_query = new WP_Query( 'category_name=whats-on&showposts=5' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="index-thumb"><?php the_post_thumbnail(array(50,50), array ('class' => 'alignleft')); ?></div>
<div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<li>
<?php the_excerpt(__('(more…)')); ?>
</li>
<?php endwhile;?>
</ul>发布于 2013-06-18 04:09:28
试试这个:
$sticky=get_option('sticky_posts');
$query_args=array(
'post__in' => $sticky,
'category__in'=>array($category)
);
$the_query = new WP_Query($query_args);如http://codex.wordpress.org/Sticky_Posts所示,您可以使用rsort和array_slice获得前5个粘性帖子
发布于 2014-06-07 22:27:03
另一个答案的问题是引入了一个变量$category,您必须首先填充该变量。
下面是修改后的代码,包括如何填充变量:
<ul id="index-blog">
<?php $category_id = get_cat_ID( 'whats-on' );
$args = array(
'cat' => $category_id,
'post__in' => get_option('sticky_posts'),
);
?>
<?php $the_query = new WP_Query($args); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
// Fix some markup issues here - the children of `ul` elements must be `li` elements....
<li>
<div class="index-thumb"><?php the_post_thumbnail(array(50,50), array ('class' => 'alignleft')); ?></div>
<div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<div class="excerpt">
<?php the_excerpt(__('(more…)')); ?>
</div>
</li>
<?php endwhile;?>
</ul>https://stackoverflow.com/questions/17155498
复制相似问题