我也有同样的问题。
根据documentation:
The hierarchy for a custom taxonomy is listed below:
taxonomy-{taxonomy}-{term}.php
taxonomy-{taxonomy}.php
taxonomy.php
archive.php
index.php
所以,您使用的witch模板文件并不重要,它们都会生成相同的问题。
当我输入模板文件代码的开头时var_dump($wp_query);
, 我发现了这个:
public \'request\' => string \'SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND (
wp_term_relationships.term_taxonomy_id IN (20)
) AND wp_posts.post_type IN (\'post\', \'page\', \'attachment\') AND (wp_posts.post_status = \'publish\' OR wp_posts.post_author = 1 AND wp_posts.post_status = \'private\') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10\' (length=433)
public \'posts\' =>
此查询中的重要部分是
wp_posts.post_type IN (\'post\', \'page\', \'attachment\')
.
出现问题的原因是没有您的自定义post_type
在此阵列中(\'post\', \'page\', \'attachment\')
.
我不知道为什么会这样,但可以使用pre_get_posts
挂钩:
add_filter(\'pre_get_posts\', \'add_custom_post_type_to_query\');
function add_custom_post_type_to_query($query) {
// We do not want unintended consequences.
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
// Check if custom taxonomy is being viewed
if( is_tax() && empty( $query->query_vars[\'suppress_filters\'] ) )
{
$query->set( \'post_type\', array(
\'post\',
\'page\',
\'my_custom_post_type\'
) );
}
}