繁体   English   中英

显示来自特定WordPress类别的特色帖子

[英]Display featured posts from a specific WordPress category

我写了一些代码,显示特定类别的最新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>

尝试这个:

$sticky=get_option('sticky_posts');
$query_args=array(
'post__in' => $sticky,
'category__in'=>array($category)
 );
$the_query = new WP_Query($query_args);

您可以使用rsort和array_slice获得排名前5位的即时贴,如http://codex.wordpress.org/Sticky_Posts中所示

另一个答案的问题是引入了变量- $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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM