WordPress wp_query精选帖子

时间:2017-08-29 14:16:01

标签: php wordpress

我正在使用WordPress网站,而且我正在处理客户端的请求功能。客户希望能够功能某些帖子,以便它们显示在列表的顶部。为了方便我的客户,我创建了一个精选类别。因此,当客户希望发布帖子时,他们所要做的就是将精选类别添加到帖子中。

以下是我当前查询,以显示类别名称为事件的所有帖子:

$query = new WP_Query( array( 'category_name' => array('events'), 'posts_per_page' => '1' ) );
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            $postLink = get_permalink();
            echo '<li class="wp-post">
                      <h5><a href="'.$postLink.'">'.get_the_title().'</a></h5>
                      <p>'.get_the_excerpt().'</p>
                      <div class="wp-post-details">
                          <span class="post-date">'.get_the_date().'</span>
                      </div>
                </li>';
        }
        echo '<li><a href="/categories/events/" class="btn">View All Events</a></li>';
        wp_reset_postdata();
    } else {
        echo '<li>No Posts Found</li>';
    }

我想更改它,因此它会首先显示活动帖子,其中包含精选的其他类别。我在谷歌和这里做了一些搜索。但到目前为止,我还没有找到适用于我的实例的解决方案。

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。在回显HTML之前,我只需要添加if (in_category('feature')) {...