我有一个名为“tours”的自定义帖子类型和名为“tourtypes”的自定义分类法,因此我可以将其分类为“cultural tour”、“sports tour”等术语。
我不是这个主题的原始作者,也不太懂分类学。但我想扩展它,这样用户就可以选择他们想要的旅游类别,而不是输出所有的“旅游”帖子。
在主题模板分类tourtypes中。php这是显示有效术语标题的代码的一部分:
<?php $terms = get_the_terms($post->ID, \'tourtypes\'); foreach ($terms as $term){ ;?>
<h2><?php echo $term->name; ?> Category</h2><?php } ;?>
但是,当涉及到我想查询指定术语的部分时,它不起作用。
<?php
$paged = get_query_string_paged();
$counter = 1;
$termg = wp_get_post_terms($post->ID, \'tourtypes\', array("fields" => "all"));
$posts_per_page = get_option(\'theme_show_portfolio_items\');
if($posts_per_page == "") {
$posts_per_page = get_option(\'posts_per_page\');
}
<?php
$paged = get_query_string_paged();
$counter = 1;
$termg = wp_get_post_terms($post->ID, \'tourtypes\', array("fields" => "all"));
$posts_per_page = get_option(\'theme_show_portfolio_items\');
if($posts_per_page == "") {
$posts_per_page = get_option(\'posts_per_page\');
}
$my_query = new WP_Query(array(\'post_type\' => \'tours\', \'tourtypes\' =>\'$termg\' ,\'paged\' => $paged, \'posts_per_page\' => $posts_per_page));
...
如果我输入slug这个词,我就能使它工作。
$my_query = new WP_Query(array(\'post_type\' => \'tours\', \'tourtypes\' =>\'cultural\' ...
我确信我只需要对wp\\u查询进行一些更改。任何人都想指出正确的代码。。我差点就放弃了
SO网友:Alvic
对于自定义分类查询,请添加“tax\\u query”=>数组()
$args = array(
\'post_type\' => \'featured_job\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 9999999,
\'orderby\' => \'date\',
\'order\' => \'DES\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'job_category\', // custom taxonomy
\'field\' => \'slug\',
\'terms\' => \'business\', // taxonomy term (category)
),
),
);