分享各种网络资源
是我乃至整个互联网的精髓所在
最新公告:
  • 开启被动注册模式,详情点击这里,同时接小单,QQ:330733312
  • 目前站点正在不断折腾和调试中,如有问题请见谅
     您目前所在位置: 站点首页 » WordPress » WordPress 代码 » WordPress获取指定分类下标签
  • WordPress获取指定分类下标签

    WordPress获取指定分类下标签-BanYuner

    今天想给站点添加一点新的功能,功能方面就是在分类下面显示该分类的下所有的标签,这样的话,可能会更加的方便访客,同时也可以提高用户对于其他页面的点击率,提高PV值等等。

    那么我就开始寻找,最终在露兜博客上面看到了相关代码,决定使用起来,后续好好利用起来并且添加到指定位置,方便用户使用。

    下面我就附上相关代码了,大家看看是否需要使用,有需要的话可以自行添加:

    function ludou_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;
    }
    

    上面的代码是添加到functions.php文件中,其次我们还需要下面的代码进行配合使用:

    // 12,13是分类ID,多个用半角逗号隔开
    $args = array( 'categories' => '12,13');
    // 调用上面定义的函数,来获取ID为12,13的分类的关联标签
    $tags = ludou_get_category_tags($args);
    // 输出我们获取到的关联标签,以列表形式打印
    $content .= "<ul>";
    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;
    

    同时,露兜博客非常贴心的提供了某个标签关联的分类的功能,也就是说,通过标签关联分类以及通过分类关联标签是互补的:

    function ludou_get_tag_categories($args) {
    global $wpdb;
    $categories = $wpdb->get_results
    ("
    SELECT DISTINCT terms1.term_id as cat_id
    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 terms2.term_id IN (".$args['tags'].") AND
    t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
    AND p1.ID = p2.ID
    ORDER by cat_id
    ");
    $count = 0;
    if($categories) {
    foreach ($categories as $category) {
    $mycategory[$count] = get_term_by('id', $category->cat_id, 'category');
    $count++;
    }
    }
    else {
    $mycategory = NULL;
    }
    return $mycategory;
    }
    

    同样的,上面的代码依然需要添加到functions.php中去,同样需要代码去实现功能:

    // 12,13是标签ID,多个用半角逗号隔开
    $args = array( 'tags' => '12,13');
    // 调用上面定义的函数,来获取ID为12,13的标签的关联分类
    $categories = ludou_get_tag_categories($args);
    // 输出我们获取到的关联分类,以列表形式打印
    $content .= "<ul>";
    if(!empty($categories)) {
    foreach ($categories as $category) {
    $content .= "<li><a href=\"".get_category_link( $category->term_id )."\">".$category->name."</a></li>";
    }
    }
    $content .= "</ul>";
    echo $content;
    

    关注我们 打赏 分享此文

    微信扫码,关注我们

    扫码,支持我

    WordPress获取指定分类下标签

    支付宝(点击图片切换)

    扫码,支持我

    WordPress获取指定分类下标签

    微信(点击图片切换)

    扫码,手机看

    WordPress获取指定分类下标签
    匿名

    个人评价:┃草根博主┃站点无技术含量┃文章内容不够吸引人┃转载内容随处可见┃资源均来自网络┃分享是我的宗旨,也是这个站点存在意义,分享的东西你现在可能不需要,但是我相信只要在某个时间你需要的时候能在这里找到,那么我就是成功的

    WordPress获取指定分类下标签
    4 + 2 =