百度云资源
百度云分享资源网站

WordPressseo优化:wordpress不同栏目如何分别调用相关tag标签?

“瓜分社交电商千亿红利,粉象生活团队招募!”

WordPress tag标签功能其实非常强大,我们可以在分类栏目页中调用当前分类文章中添加的所有标签,以方便用户检索自己喜欢的内容,提高用户体验度,利于网站seo优化。

wordpress不同栏目如何分别调用相关tag标签?

首先,在主题functions.php模板文件中添加以下函数:

function get_category_tags($args) {
    global $wpdb;
    $tags = $wpdb->get_results
    ("
        SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name
        FROM
            $wpdb->posts as p1
            LEFT JOIN $wpdb->term_relationships as r1 ON p1.ID = r1.object_ID
            LEFT JOIN $wpdb->term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
            LEFT JOIN $wpdb->terms as terms1 ON t1.term_id = terms1.term_id,
            $wpdb->posts as p2
            LEFT JOIN $wpdb->term_relationships as r2 ON p2.ID = r2.object_ID
            LEFT JOIN $wpdb->term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
            LEFT JOIN $wpdb->terms as terms2 ON t2.term_id = terms2.term_id
        WHERE
            t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
            t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
            AND p1.ID = p2.ID
        ORDER by tag_name
    ");
    $count = 0;
    if($tags) {
        foreach ($tags as $tag) {
            $mytag[$count] = get_term_by('id', $tag->tag_id, 'post_tag');
            $count++;
        }
    } else {
      $mytag = NULL;
    }
    return $mytag;
}

其次,将下面调用输出代码,添加到主题category.php模板适当位置:

<?php
    $cat= single_cat_title('', false);
    $args = array( 'categories' => get_cat_ID($cat));
    $tags = get_category_tags($args);
    $content .= "<ul class='cat-tag'>";
    if(!empty($tags)) {
        foreach ($tags as $tag) {
            $content .= "<li><a href=\"".get_tag_link($tag->term_id)."\">".$tag->name."</a></li>";
        }
    }
    $content .= "</ul>";
    echo $content;
?>

最后,再适当加上样式即可:

.cat-tag{
    float: left;
    width: 100%;
}
.cat-tag li a{
    float: left;
    margin: 0 5px;
}

懂css的同学,可以自己调节样式,达到自己想要的展示效果!本博WordPress 分类栏目页展示关联TAG如下:

wordpress不同栏目如何分别调用相关tag标签?

 

赞(2)
未经允许不得转载:木木资源博 » WordPressseo优化:wordpress不同栏目如何分别调用相关tag标签?
分享到: 更多 (0)
##分享一个宝妈兼职项目,坚持半年,你也可以每月躺赚10000+#

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

木木网赚博客 分享网络营销技巧

关于本博博主自留地