我想循环浏览类别4中的所有帖子,但为什么在指定post\\u type post后结果中仍然有页面?
$homeNeWs = new WP_Query(array(
\'post_type\' => \'post\',
\'posts_per_page\' => 6,
\'cat\' => 4
));
while ($homeNeWs->have_posts()) : $homeNeWs->the_post();
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php
endwhile;
wp_reset_postdata();
?>
我确实添加了一个函数,使页面具有类别和标记,基于
http://spicemailer.com/wordpress/add-categories-tags-pages-wordpress/function taxo_for_page() {
register_taxonomy_for_object_type(\'post_tag\', \'page\');
register_taxonomy_for_object_type(\'category\', \'page\');
}
add_action(\'init\', \'taxo_for_page\');
if (! is_admin()) {
add_action(\'pre_get_posts\', \'cat_n_tag_archives\');
}
function cat_n_tag_archives($wp_query) {
$ary_one = array(\'post\', \'page\');
if ($wp_query->get(\'category_name\') || $wp_query->get(\'cat\'))
$wp_query->set(\'post_type\', $ary_one);
if ($wp_query->get(\'tag\'))
$wp_query->set( \'post_type\', $ary_one );
}
我是否需要为上述代码添加更多内容?
最合适的回答,由SO网友:s_ha_dum 整理而成
你的cat_n_tag_archives()
功能是向所有类别和标记结果不加选择地添加页面。您可能想要(我也有一点猜测)的是将该过滤器仅限于主查询:
function cat_n_tag_archives($wp_query) {
if (!$wp_query->is_main_query()) return; // This bit checks for the main query
$ary_one = array(\'post\', \'page\');
if ($wp_query->get(\'category_name\') || $wp_query->get(\'cat\'))
$wp_query->set(\'post_type\', $ary_one);
if ($wp_query->get(\'tag\'))
$wp_query->set( \'post_type\', $ary_one );
}