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

一弯拱桥对映着一弯晓月,桥下流水波光粼粼,昙花一般的月,在我眼中异香腾风,秀色媚景。夜的静谧是风景,昼的明媚是风景。江花胜火红,寒山伤心碧,空伫玉阶白,西风残照黄。处处皆风景,处处皆绝妙风景。

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

关于 文章形式,在以后的好代码教程中,倡萌会继续补充,今天分享下 WordPress 首页排除某些文章形式的方法,将下面的代码添加到主题的 functions.php 即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * 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' );

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

以上就是WordPress 首页排除某些文章形式(Post Formats)。做你没做过的事情叫成长,做你不愿意做的事情叫改变,做你不敢做的事情叫突破。更多关于WordPress 首页排除某些文章形式(Post Formats)请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
WordPress站点Gravatar头像前后台不显示的如何解决办法

WordPress主题需要支持https吗?WordPress站点如何如何实现https?

WordPress站点的页面/标签/分类URL地址如何添加.html?

WordPress站点更换了域名后数据库应该如何操作替换新旧域名?

WordPress安装在主机空间的什么目录里面?根目录在哪里?