Wordpress获取类别标签

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Wordpress获取类别标签相关的知识,希望对你有一定的参考价值。

Verified working for WP v2.9.1
  1. // In your theme's functions.php insert the following function:
  2.  
  3. function get_category_tags($args) {
  4. global $wpdb;
  5. $tags = $wpdb->get_results
  6. ("
  7. SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, null as tag_link
  8. FROM
  9. wp_posts as p1
  10. LEFT JOIN wp_term_relationships as r1 ON p1.ID = r1.object_ID
  11. LEFT JOIN wp_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
  12. LEFT JOIN wp_terms as terms1 ON t1.term_id = terms1.term_id,
  13.  
  14. wp_posts as p2
  15. LEFT JOIN wp_term_relationships as r2 ON p2.ID = r2.object_ID
  16. LEFT JOIN wp_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
  17. LEFT JOIN wp_terms as terms2 ON t2.term_id = terms2.term_id
  18. WHERE
  19. t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
  20. t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
  21. AND p1.ID = p2.ID
  22. ORDER by tag_name
  23. ");
  24. $count = 0;
  25. foreach ($tags as $tag) {
  26. $tags[$count]->tag_link = get_tag_link($tag->tag_id);
  27. $count++;
  28. }
  29. return $tags;
  30. }
  31.  
  32. // In your theme document call the function as follows. Notice it accepts multiple category id's:
  33.  
  34. $args = array(
  35. 'categories' => '12,13,14'
  36. );
  37. $tags = get_category_tags($args);
  38.  
  39. // This will return an array that you could do the following with:
  40.  
  41. $content .= "<ul>";
  42. foreach ($tags as $tag) {
  43. $content .= "<li><a href="$tag->tag_link">$tag->tag_name</a></li>";
  44. }
  45. $content .= "</ul>";
  46. echo $content;

以上是关于Wordpress获取类别标签的主要内容,如果未能解决你的问题,请参考以下文章

PHP 获取自定义帖子类型的WordPress标签/类别

获取自定义帖子类型的WordPress标签/类别

如何在 Gatsby 中获取所有 Wordpress 帖子和显示标签

从 WordPress 中的特定类别获取置顶帖子

WordPress > 从自定义帖子类型获取自定义分类

手动检索带有 JSON 类别/标签的 Wordpress 帖子