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

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

160次阅读

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

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

作者:管理员  修订  2019/07/10 11:51:28

WP爱好者专注WordPress在线系统培训,10年来超过十万学员在此获得技术提升和人脉圈子。

WordPress课程已融入到移动搜索、零基础建站、WP爱好者开源主题模板、SEM入门等。

欢迎添加助教微信:tc199361,马上获得优秀学员案例。