我想在single.php上使用wp_tag_cloud(),它使用從特定類別獲取所有標籤的參數,包括其子類別中的所有標籤,帖子。獲取所有基於特定類別的標籤(包括來自子類別和帖子的所有標籤)wordpress
回答
我得到這個接近。
<div class="tag_cloud_on_single">
<h2>Popular Topics</h2>
<?php
$category = get_the_category();
$root_cat_of_curr = $category[0]->category_parent;
function get_cat_slug($cat_id) {
$cat_id = (int) $cat_id;
$category = &get_category($cat_id);
return $category->slug;
}
$my_cat = get_cat_slug($root_cat_of_curr);
$custom_query = new WP_Query('posts_per_page=-1&category_name='.$my_cat.'');
if ($custom_query->have_posts()) :
while ($custom_query->have_posts()) : $custom_query->the_post();
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$all_tags[] = $tag->term_id;
}
}
endwhile;
endif;
$tags_arr = array_unique($all_tags);
$tags_str = implode(",", $tags_arr);
$args = array(
'smallest' => 12,
'largest' => 24,
'unit' => 'pt',
'number' => 0,
'format' => 'flat',
'separator' => " ",
'orderby' => 'name',
'order' => 'RAND',
'exclude' => null,
'topic_count_text_callback' => default_topic_count_text,
'link' => 'view',
'echo' => true,
'include' => $tags_str
);
wp_tag_cloud($args);
?>
</div>
謝謝大家的貢獻。感謝你的幫助。
啊,先前沒有意識到有「包含」的說法。智能移動放入指定的標籤。 –
在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);
我意識到我在回答中脫離主題。這將返回所有標籤......但不是標籤雲。讓我來研究它的這個方面。 –
添加了第二個功能來輸出標籤雲。它沒有很多功能,但它可以很容易地根據自己的喜好進行擴展/修改。 –
我真的很感謝你的工作和時間。我測試了你的代碼,但不幸的是這不是我問的。它反映了整個網站的所有標籤。 –
在你的主題的功能.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, null as tag_link
FROM
wp_posts as p1
LEFT JOIN wp_term_relationships as r1 ON p1.ID = r1.object_ID
LEFT JOIN wp_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
LEFT JOIN wp_terms as terms1 ON t1.term_id = terms1.term_id,
wp_posts as p2
LEFT JOIN wp_term_relationships as r2 ON p2.ID = r2.object_ID
LEFT JOIN wp_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
LEFT JOIN wp_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;
foreach ($tags as $tag) {
$tags[$count]->tag_link = get_tag_link($tag->tag_id);
$count++;
}
return $tags;
}
在您的主題文檔中調用如下函數。注意它接受多個類別的ID:
$args = array(
'categories' => '12,13,14'
);
$tags = get_category_tags($args);
這將返回一個數組,你可以做以下幾點:
$content .= "<ul>";
foreach ($tags as $tag) {
$content .= "<li><a href=\"$tag->tag_link\">$tag->tag_name</a></li>";
}
$content .= "</ul>";
echo $content;
- 1. WordPress獲取所有類別的帖子
- 2. wp類別顯示所有類別的所有帖子,而不是來自特定類別的特定帖子
- 3. WordPress無法獲取尊重標籤和類別條件的所有帖子
- 4. 如何獲取所有類別的所有帖子自定義帖子類型?
- 5. 基於帖子類別的自定義body標籤
- 6. WordPress的:從特定類別的帖子生成標籤陣列
- 7. Wordpress API,帶有類別和標籤的新帖子
- 8. WordPress網址用於顯示帖子的類別和標籤
- 9. 獲取WordPress類別中的帖子總數(包括子類)
- 10. WordPress的類別模板顯示所有類別的帖子,而不是特定的類別帖子
- 11. WordPress的 - 顯示所有帖子+那些與特定的標籤
- 12. 特定類別的所有WordPress的帖子將在page.php文件
- 13. 自定義帖子類型獲取錯誤的類別和標籤
- 14. Wordpress顯示特定類別下的所有帖子
- 15. 用特定的類別和標籤計算帖子laravel
- 16. 顯示Wordpress中特定父類別的子類別下的所有帖子標題
- 17. 如何獲取WordPress中的所有帖子標籤?
- 18. 從Wordpress中的所有類別獲取帖子
- 19. 奇怪的行爲從wordpress類別獲取所有帖子
- 20. 使用PHP獲取所有父類別的wordpress帖子
- 21. Wordpress sql:獲取所有類別的帖子
- 22. WordPress的類別,獲取其所有帖子
- 23. 如何查詢自定義WordPress類別中的所有帖子
- 24. WordPress的標籤 - 如何獲得具有相同標籤的所有帖子
- 25. 從分類中獲得所有類別的所有帖子
- 26. 關於帖子標題和類別的Wordpress自定義查詢
- 27. 如何在WordPress中選擇包含特定標籤/類別的帖子
- 28. 如何選擇所有包含特定標籤的帖子?
- 29. WordPress的子類別與帖子標題
- 30. 如何通過mysql獲取wordpress中所有類別的所有帖子?
這是一個很好的問題。 –