wordpress怎么获取指定分类文章下的所有标签

小初seo 网站建设评论1,076字数 4228阅读14分5秒阅读模式
标签不依赖于类别,它们是分开的。话虽这么说,要使某个特定类别的所有标签都在使用中,唯一的方法是循环浏览该类别的每个帖子并获取每个帖子的标签

WordPress中没有本地方法可以做到这一点。原因是标签不依赖于类别,它们是分开的。话虽这么说,要使某个特定类别的所有标签都在使用中,唯一的方法是循环浏览该类别的每个帖子并获取每个帖子的标签。

我已经编写了一个快速功能来做到这一点。

(包括子类别和帖子中的所有标签)

将此函数放入您的functions.php文件中。

function get_tags_in_use($category_ID, $type = 'name'){
// Set up the query for our posts
$my_posts = new WP_Query(array(
'cat' => $category_ID, // Your category id
'posts_per_page' => -1 // All posts from that category
));
// Initialize our tag arrays
$tags_by_id = array();
$tags_by_name = array();
$tags_by_slug = array();
// If there are posts in this category, loop through them
if ($my_posts->have_posts()): while ($my_posts->have_posts()): $my_posts->the_post();
// Get all tags of current post
$post_tags = wp_get_post_tags($my_posts->post->ID);
// Loop through each tag
foreach ($post_tags as $tag):
// Set up our tags by id, name, and/or slug
$tag_id = $tag->term_id;
$tag_name = $tag->name;
$tag_slug = $tag->slug;
// Push each tag into our main array if not already in it
if (!in_array($tag_id, $tags_by_id))
array_push($tags_by_id, $tag_id);
if (!in_array($tag_name, $tags_by_name))
array_push($tags_by_name, $tag_name);
if (!in_array($tag_slug, $tags_by_slug))
array_push($tags_by_slug, $tag_slug);
endforeach;
endwhile; endif;
// Return value specified
if ($type == 'id')
return $tags_by_id;
if ($type == 'name')
return $tags_by_name;
if ($type == 'slug')
return $tags_by_slug;
}

 

然后,当您要获取特定类别的标签时,请像下面这样调用此函数:

// First paramater is the category and the second paramater is how to return the tag (by name, by id, or by slug)
// Leave second paramater blank to default to name
$tags = get_tags_in_use(59, 'name');

 

希望这可以帮助。

编辑:

这是您需要与其他功能结合使用的功能:

function tag_cloud_by_category($category_ID){
// Get our tag array
$tags = get_tags_in_use($category_ID, 'id');
// Start our output variable
echo '<div class="tag-cloud">';
// Cycle through each tag and set it up
foreach ($tags as $tag):
// Get our count
$term = get_term_by('id', $tag, 'post_tag');
$count = $term->count;
// Get tag name
$tag_info = get_tag($tag);
$tag_name = $tag_info->name;
// Get tag link
$tag_link = get_tag_link($tag);
// Set up our font size based on count
$size = 8 + $count;
echo '<span style="font-size:'.$size.'px;">';
echo '<a href="'.$tag_link.'">'.$tag_name.'</a>';
echo ' </span>';
endforeach;
echo '</div>';
}

 

因此,您可以像下面这样简单地使用此功能:

tag_cloud_by_category($cat_id);

 

想显示wordpress标签下的所有文章,因为我当初没有新建wordpress这个分类目录,所以只能用标签获取。

用wordpress默认的get_tags()获取函数可以实现,实现方法如下:

<?php
    // 得到所有标签列表(57为标签id,想获取某个标签只需添加进去用逗号隔开,如'include' => '13,57')
    $args=array(
        'include' => '57'
    );
    $tags = get_tags($args);
    // 循环所有标签 
    foreach ($tags as $tag) {
        // 得到标签ID 
        $tagid = $tag->term_id;
        // 得到标签下所有文章 
        query_posts("showposts=-1&tag_id=$tagid");
?>
<!-- 输出标签标题及链接 -->
<h2>标签: <a href="<?php echo get_tag_link($tagid);?>" title="<?php echo $tag->name?>"><?php echo $tag->name; ?></a></h2>
<!-- 输出所有文章的标题及链接 -->
<ul>
    <?php while (have_posts()) : the_post(); ?>
    <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"> <?php the_title(); ?></a></li>
    <?php endwhile; ?>
</ul>
<?php } ?>

如果需要在分类列表页面,显示当前分类文章中添加的所有标签,方便读者阅读自己喜欢的内容,下面的代码可以帮你实现这个功能。

首先,在主题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;
}

其次,将下面调用输出代码,添加到主题archive.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;
?>

个人感觉放到头部调用函数:

<?php get_header(); ?>

下面比较合适。

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

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

 最后更新:2021-6-4
  • 本文由 小初seo 发表于 2021年5月13日10:45:09
  • 转载请务必保留本文链接:https://www.pkak.cn/web/3964.html
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证