WordPress 首页排除某些文章形式(Post Formats)

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' );

注:请根据自己的需要保留所需的文章形式

来源:

https://www.wpdaxue.com/exclude-post-formats-from-homepage.html

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?