很抱歉重新发布这篇文章,但它出现在头版,我注意到它太旧了,太晚了。。。以下是我对这个问题的看法:
// This will filter the shortcode attributes and will insert custom
// value for the "cat" parameter
function filter_gmaps_shortcode_atts( $atts ) {
// We add a custom value in the $cat parameter
if ( is_tax( \'cities\' ) ) {
$atts[\'cat\'] = \'filter_taxonomy_cities\';
} elseif ( is_tax( \'regions\' ) ) {
$atts[\'cat\'] = \'filter_taxonomy_regions\';
}
return $atts;
}
add_filter( \'gmshc_shortcode_atts\', \'filter_gmaps_shortcode_atts\', 10 );
// This filters the WordPress query and checks for our custom values from above
// We then modify the query to look for the proper post type and taxonomy
function filter_gmaps_get_post( &$wp_query ) {
if ( isset( $wp_query->query_vars[\'category__in\'] ) ) {
$queried_obj = get_queried_object();
if ( in_array( \'filter_taxonomy_cities\', $wp_query->query_vars[\'category__in\'] ) || in_array( \'filter_taxonomy_regions\', $wp_query->query_vars[\'category__in\'] ) ) {
unset( $wp_query->query_vars[\'category__in\'] );
$wp_query->query_vars[\'tax_query\'] = array(
array(
\'taxonomy\' => $queried_obj->taxonomy,
\'terms\' => array( intval( $queried_obj->term_id ) ),
\'field\' => \'id\'
)
);
$wp_query->query_vars[\'post_type\'] = \'hotels\';
}
}
}
add_action( \'pre_get_posts\', \'filter_gmaps_get_post\', 10 );
基本上,我们在“城市”或“地区”分类页面上过滤短代码属性,并为“cat”参数添加自定义值。在
pre_get_posts
操作激发者
WP_Query::get_posts()
我们检查自定义值是否存在于
category__in
参数-如果是,则取消设置
category__in
参数并添加
tax_query
当前分类的参数。