仅显示页面中具有特定标记的帖子

时间:2015-09-02 06:24:37

标签: php wordpress

我正在尝试仅显示带有特定标记(特色)的帖子到页面(page-featured.php),但没有显示任何内容。

的functions.php

function display_featured( $query ) {
    if ( $query->is_category() && $query->query_vars['category_name'] == 'featured') {
        $query->set( 'tag', 'featured' );
    }
}
add_action( 'pre_get_posts', 'display_featured' );

页面featured.php

<?php if ( have_posts() ) : ?>
<?php  ?>
<?php add_action( 'pre_get_posts', 'display_featured' ); ?>
<?php while ( have_posts() ) : the_post(); ?>

content here

<?php endwhile; else : ?>
no post found
<?php endif; ?>

有没有更好的方法来实际运作?感谢。

1 个答案:

答案 0 :(得分:0)

修改function.php

中的代码
function display_featured( $query ) {

    if ( is_page('featured')) {  // Page ID, Page Title or Page Slug 
        $query->set( 'tag', 'featured' );
    }
}
add_action( 'pre_get_posts', 'display_featured' );