禁用每个帖子类型的提要但保持已启用\\u存档的最佳方法是什么?
如何使用HAS_ARCHIVE但禁用每个帖子类型的提要?
我今天遇到了这个问题。我不知道这是否是最好的方法,但下面是我如何解决它的has_archive
当然,仍设置为true):
// First we remove WP default feed actions
// If we stop here, feeds would be disabled altogether
remove_action(\'do_feed_rdf\', \'do_feed_rdf\', 10, 1);
remove_action(\'do_feed_rss\', \'do_feed_rss\', 10, 1);
remove_action(\'do_feed_rss2\', \'do_feed_rss2\', 10, 1);
remove_action(\'do_feed_atom\', \'do_feed_atom\', 10, 1);
// Now we add our own actions, which point to our own feed function
add_action(\'do_feed_rdf\', \'my_do_feed\', 10, 1);
add_action(\'do_feed_rss\', \'my_do_feed\', 10, 1);
add_action(\'do_feed_rss2\', \'my_do_feed\', 10, 1);
add_action(\'do_feed_atom\', \'my_do_feed\', 10, 1);
// Finally, we do the post type check, and generate feeds conditionally
function my_do_feed() {
global $wp_query;
$no_feed = array(\'cpt_1\', \'cpt_2\');
if(in_array($wp_query->query_vars[\'post_type\'], $no_feed)) {
wp_die(__(\'This is not a valid feed address.\', \'textdomain\'));
}
else {
do_feed_rss2($wp_query->is_comment_feed);
}
}
请注意,这将导致所有提要在生成时都生成为RSS 2.0,但您可以大致了解。我只是通过检查归档页面上是否有一系列自定义帖子类型,然后删除提要链接操作,在一个主题中实现了这一点:
function themename_remove_feed_links() {
if ( is_post_type_archive( array( \'gallery\', \'client\', \'testimonial\', \'slideshow\' ) ) ) {
remove_action(\'wp_head\', \'feed_links_extra\', 3 );
remove_action(\'wp_head\', \'feed_links\', 2 );
}
}
add_action( \'template_redirect\', \'themename_remove_feed_links\' );
我还没有测试,但应该简单到:
register_post_type( \'slug\', array(
\'has_archive\' => true,
\'rewrite\' => array(
\'slug\' => \'slug\',
\'feeds\' => false,
),
);