外贸独立站的底层设计密码WordPress 成品站模板
当前位置:首页>WordPress建站>WordPress开发>WordPress 首页排除某些文章形式(Post Formats)

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

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
WordPress开发

WordPress 为自定义表格/列表添加分页导航功能

2013-9-9 10:37:26

WordPress开发

WordPress 个人资料添加额外的字段

2013-9-20 9:45:30

2 条回复 A文章作者 M管理员
  1. 能不能直接隐藏 形式?

  2. 我用这个怎么不管用呢?
    我搞了个子主题,把这些代码放在function.php里:
    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’ );

    还有什么会影响到这个钩子吗?

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索