首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示来自特定WordPress类别的特色帖子

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

Stack Overflow用户
提问于 2013-06-18 03:43:28
回答 2查看 588关注 0票数 0

我写了一些代码来显示来自特定类别的最新5个帖子,但是我不知道如何让它显示该类别中标记为仅有特色的最新5个帖子。我的意思是这个帖子已经被贴上了,所以基本上它会显示每个类别中被贴上的5个帖子。

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

发布于 2013-06-18 04:09:28

试试这个:

代码语言:javascript
复制
$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个粘性帖子

票数 0
EN

Stack Overflow用户

发布于 2014-06-07 22:27:03

另一个答案的问题是引入了一个变量$category,您必须首先填充该变量。

下面是修改后的代码,包括如何填充变量:

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17155498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档