您需要使用pre\\u get\\u posts挂钩。其中,您可以在其他类别页面上包含默认类别帖子。
function wpse_pre_get_posts( $q )
{
// its the main query, and its the category page archive
if( $q->is_main_query() && $q->is_category() ) :
// get the default selected category id, fallback to 1 (default one id)
$default_cat = (int) get_option(\'default_category\', 1);
// if the category page is not the default category page
if( $default_cat != $q->get_queried_object_id() ):
// get the already used tax_query arguments
$tax_query = $q->get(\'tax_query\');
// add the default one
$tax_query[] = array(
\'taxonomy\' => \'category\',
\'field\' => \'term_id\',
\'terms\' => $default_cat
);
// relation should be or, which means either the current category or the default
$tax_query[\'relation\'] = \'OR\';
// set the values to query
$q->set(\'tax_query\', $tax_query );
endif;
endif;
}
add_action( \'pre_get_posts\', \'wpse_pre_get_posts\' );
好的,现在这可能与wp\\u query变量冲突,所以下一个解决方案将使用post\\u请求文件管理器,它用于过滤原始查询。
function wpse_posts_request( $request, $q )
{
global $wpdb;
// its the main query, and its the category page archive
if( $q->is_main_query() && $q->is_category() ) :
// get the default selected category id, fallback to 1 (default one id)
$default_cat = (int) get_option(\'default_category\', 1);
// category now
$cat_now = $q->get(\'cat\');
// if the category page is not the default category page
if( $default_cat != $cat_now ):
$request = str_replace("$wpdb->term_relationships.term_taxonomy_id IN ($cat_now)", "$wpdb->term_relationships.term_taxonomy_id IN ($cat_now, $default_cat)", $request);
endif;
endif;
return $request;
}
add_filter( \'posts_request\', \'wpse_posts_request\', 10, 2 );
就是这样。你都做完了