WordPress 3.1引入了 文章形式(Post Formats),它是文章的一种属性,可以被主题用来决定文章的显示方式。简单地说,如果你使用的主题支持“文章形式”(也就是主题作者已经为不同的文章形式定制了不同的显示样式),你就可以从一个单选列表中选择文章的形式,以此决定文章的显示样式。

关于 文章形式,在以后的教程中,倡萌会继续补充,今天分享下 WordPress 首页排除某些文章形式的方法,将下面的代码添加到主题的 functions.php 即可:
/**
* WordPress 首页排除某些文章形式(Post Formats)
* https://www.wpdaxue.com/exclude-post-formats-from-homepage.html
*/
function exclude_post_formats_from_homepage( $query ) {
if( $query->is_main_query() && $query->is_home() ) { //判断首页主查询
$tax_query = array( array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
//请根据需要保留要排除的文章形式
'post-format-aside',
'post-format-audio',
'post-format-chat',
'post-format-gallery',
'post-format-image',
'post-format-link',
'post-format-quote',
'post-format-status',
'post-format-video'
),
'operator' => 'NOT IN',
) );
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'exclude_post_formats_from_homepage' ); |
/**
* WordPress 首页排除某些文章形式(Post Formats)
* https://www.wpdaxue.com/exclude-post-formats-from-homepage.html
*/
function exclude_post_formats_from_homepage( $query ) {
if( $query->is_main_query() && $query->is_home() ) { //判断首页主查询
$tax_query = array( array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
//请根据需要保留要排除的文章形式
'post-format-aside',
'post-format-audio',
'post-format-chat',
'post-format-gallery',
'post-format-image',
'post-format-link',
'post-format-quote',
'post-format-status',
'post-format-video'
),
'operator' => 'NOT IN',
) );
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'exclude_post_formats_from_homepage' );
注:请根据自己的需要保留所需的文章形式
来源:
https://www.wpdaxue.com/exclude-post-formats-from-homepage.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32255.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57