我有两个cpt,事件和报告,它们都共享分类位置,我有我的分类位置。php,但这是显示报告&;位置事件,我只想显示位置报告。有人能帮我解决这个问题吗?
谢谢
我有两个cpt,事件和报告,它们都共享分类位置,我有我的分类位置。php,但这是显示报告&;位置事件,我只想显示位置报告。有人能帮我解决这个问题吗?
谢谢
在您的taxonomy-locations.php
文件
global $wp_query;
$args = array_merge( $wp_query->query, array( \'post_type\' => \'report\' ) );
query_posts( $args );
或者,您可以使用适当的钩子修改查询(这将是最有效的方法),但由于它是为大多数查询运行的,因此您需要检查它实际上是一个要将post type设置为“report”的查询。例如:function my_restrict_to_report() {
//And any other checks
if (!is_admin() && is_tax(\'location\')) {
set_query_var(\'post_type\',\'report\');
}
}
add_action( \'pre_get_posts\', \'my_restrict_to_report\' );
未测试我相信您也可以通过pre\\u get\\u posts操作来实现这一点。$query变量是整个查询对象,它是“通过引用”传递的,这意味着在函数中对它所做的任何操作都将修改原始查询
add_action(\'pre_get_posts\', \'kia_limit_tax\' );
function kia_limit_tax( $query ) {
//check that we\'re in the locations taxonomy before modifying the query
if(is_tax(\'locations\')){
set_query_var(\'post_type\',\'event\');
//which is the more elegant way to write this:
//$query->set(\'post_type\',\'event\');
}
}
is\\U taxonomy()已弃用,应使用is\\U tax()http://codex.wordpress.org/Function_Reference/is_tax