【问题标题】:Display featured posts from a specific WordPress category显示来自特定 WordPress 类别的精选帖子
【发布时间】:2013-06-17 19:43:28
【问题描述】:

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

【问题讨论】:

    标签: php html css wordpress


    【解决方案1】:

    试试这个:

    $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 所示

    【讨论】:

    • 试一试仍然没有运气
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      相关资源
      最近更新 更多