我正在尝试设置分类法,以隐藏特定页面上的post\\U格式。
我已经创建了以下内容,可以阻止post\\u格式在页面上显示,但它也会将其隐藏在CMS的“Posts”页面中。有没有办法绕过这个问题?
function exclude_campaigns( $query ) {
if( $query->is_main_query() && $query->is_home() ) {
$tax_query = array( array(
\'taxonomy\' => \'post_format\',
\'field\' => \'slug\',
\'terms\' => array( \'post-format-link\' ),
\'operator\' => \'NOT IN\',
) );
$query->set( \'tax_query\', $tax_query );
}
}
add_action( \'pre_get_posts\', \'exclude_campaigns\' );
更新的功能:
// Exclude post-format from one particular page
function exclude_campaigns( $query ) {
// is_page() allows for page ID, page title or page slug
if( $query->is_main_query() && $query->is_page(239) ) {
$tax_query = array( array(
\'taxonomy\' => \'post_format\',
\'field\' => \'slug\',
\'terms\' => array( \'post-format-link\' ),
\'operator\' => \'NOT IN\',
) );
$query->set( \'tax_query\', $tax_query );
}
}
add_action( \'pre_get_posts\', \'exclude_campaigns\' );
SO网友:Courtney Ivey
如果您试图在一个特定页面上隐藏帖子格式,那么下面的函数应该可以工作。
<?php
// Exclude post-format from one particular page
function exclude_campaigns( $query ) {
// is_page() allows for page ID, page title or page slug
if( $query->is_main_query() && $query->is_page( INSERT_PAGE_ID_HERE ) ) {
$tax_query = array(
\'taxonomy\' => \'post_format\',
\'field\' => \'slug\',
\'terms\' => array( \'post-format-link\' ),
\'operator\' => \'NOT IN\',
);
$query->set( \'tax_query\', $tax_query );
}
}
add_action( \'pre_get_posts\', \'exclude_campaigns\' );