这是直接将标签作为文章关键词的,这么做也是为了方便代码实现WORDPRESS自动关键词KEYWORDS与描述DESCRIPTION。但如此常常是一个标签才对应一篇文章,为了提高用户体验,我们可以在WORDPRESS标签页面只有一篇文章时自动跳转到该文章。
将下面的代码添加到主题的FUNCTIONS.PHP文件下:
add_action('template_redirect', 'tag_redirect_single_post');
function tag_redirect_single_post() {
if (is_tag()) {
global $wp_query;
if ($wp_query->post_count == 1) {
wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
}
}
}
代码作者未知。高级一点的,可以这样:
add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
if (is_tag() || is_search()) {
global $wp_query;
if ($wp_query->post_count == 1) {
wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
}
}
本文章转自DEVEWORD(转载此文章仅用于学习交流只用,如有异议请联系本站删除,谢谢!)