如何让 WordPress 不/只显示特定的 post-format

post format 是 wordpress 新加入的功能,有了它的加入可以实现以往依靠分类和标签无法实现的功能

让主题支持 format

如果你的主题还不能支持 format,那么请将类似下面的代码加入你主题的 functions.php 中:

add_theme_support( 'post-formats', array( 'status', 'video' ) );

然后,再将下面的代码加入到你的循环中即可以了:

get_template_part( 'content', get_post_format() );

不显示指定的 formats

如何让一些特定格式的文章排除在首页,这个也许是这篇日志的重点,方法是将类似下面的代码加入你的 functions.php 中即可,不是首页的地方只需将$query后的 is_home 修改为需要的即可:

function exclude_post_formats( $query ) {
    if( $query->is_main_query() && $query->is_home() ) {
        $tax_query = array( array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => array(
                'post-format-status'
                ),
            'operator' => 'NOT IN',
            ) );
        $query->set( 'tax_query', $tax_query );
    }
}
add_action( 'pre_get_posts', 'exclude_post_formats' );

只显示指定的 formats

如果,想只显示指定的格式,那么只要把上面代码中的“NOT IN”改为“IN”就可以了。