這是一個解決方案,用於輸出包含在頁面上共享當前標籤一次的帖子的類別列表,而不是在循環內。我只在標記頁面上測試它,其中'標記'查詢變量可用。它使用自定義查詢using the $wpdb
class。 (這對學習非常有用)。
您需要在開頭定義自己的表格前綴,或設置爲''
。
測試 我測試了這個代碼,直接把它放在tag.php中。將它放在你自己的插件或主題的functions.php中會更聰明。一些調整可能是必要的,以確保$ wp_query對象在該函數內可用。
術語和分類標準 請記住,標籤和類別在wordpress數據庫中被認爲是相等的。與帖子,頁面,修訂和草稿一樣,所有「帖子」,類別和標籤都是「條款」。它們之間的區別在於它們的「分類法」,這就是該術語是「類別」還是「標籤」。
「條款」表包含所有標籤和類別。 'term_taxonomy'存儲該術語是標籤,類別還是帖子鏈接。 'term_relationships'表對帖子和分類進行配對。 'object_id'列可以保存該表中的發帖ID或術語ID。
代碼大綱
- 設置表前綴
- 獲取從query_vars
- 從術語表
- 獲取標籤的term_id標籤名稱獲取的帖子ID的(命名爲「OBJECT_ID ')從term_relationships表中作爲數字索引數組
- 將該數值數組轉換爲以逗號分隔的字符串
分
- 飾件後面的逗號
- 選擇具有從#4的帖子ID的從term_relationships作爲多維關聯數組
- 創建所有的職位具有該標籤的術語的陣列中的所有term_taxonomy_id的(類別和標記) 。
- Foreach元素在這個數組中,選擇分類法(無論是'category'還是'tag'),如果它是一個類別,則將term_id放入數組中。
- 將該數組轉換爲字符串以供下一步使用。
- 呼叫
wp_list_categories()
與category_id字符串作爲要包括的類別。
代碼示例
//define table prefix
$wp_pre = 'wp_';
//get tag name from url
$tag_name = $wp_query->get('tag');
//get term_id from database
$term_id = $wpdb->get_var("
SELECT term_id
FROM ".$wp_pre."terms
WHERE name ='".$tag_name."'");
//select post id's with this tag
$posts_with_tag = $wpdb->get_results("
SELECT object_id
FROM ".$wp_pre."term_relationships
WHERE term_taxonomy_id = '".$term_id."'",
ARRAY_N);
//make string out of returned array in an array
$posts_with_tag_as_string = implode(',',$posts_with_tag[0]);
//trim trailing comma
$posts_with_tag_as_string = rtrim($posts_with_tag_as_string,',');
//select all terms having post id
$terms_having_post = $wpdb->get_results("
SELECT term_taxonomy_id
FROM ".$wp_pre."term_relationships
WHERE object_id
IN ('".$posts_with_tag_as_string."')",
ARRAY_A);
foreach($terms_having_post as $key=>$val){
$post_terms[] = $val['term_taxonomy_id'];
}
//get taxonomy name for each term_having_post
foreach($post_terms as $term_id){
$taxonomy = $wpdb->get_var("
SELECT taxonomy
FROM ".$wp_pre."term_taxonomy
WHERE term_id = '".$term_id."'");
if($taxonomy == 'category'){
$cats[] = $term_id;
}
}
$category_ids = implode(',',$cats);
//trim trailing comma
$category_ids = rtrim($category_ids,',');
//output list of categories with the included id's.
wp_list_categories('include='.$category_ids);
我希望這對你的作品。
UPDATE 在回答您的意見,以get categories和說明等貓元數據,你可以用get_categories
替代wp_list_categories
,同時還傳遞include='.$category_ids
。這將返回一系列類別對象,您可以根據需要循環顯示類別對象,說明,帖子數量等。
新的代碼示例(從抄本頁面)
$args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name.'</a> </p> ';
echo '<p> Description:'. $category->description . '</p>';
echo '<p> Post Count: '. $category->count . '</p>';
}
這將是一個自定義查詢。由於標籤和類別只與它們共享的帖子間接相關,因此您必須循環所有帖子(或創建適當的SQL連接)才能找到所有組合。你設想什麼樣的用戶界面? – 2010-11-09 11:03:39
嗨,我添加了我的HP循環的外觀,我試圖得到的標籤頁循環應該也是類似的,顯然只輸出標籤所在的類別。 – Blackbird 2010-11-09 11:26:28
想想如果通過查詢meta的更簡單的方法鍵「is_front_page」並檢查標籤是否存在於任何這些帖子中?我不知道這一切是否可能。 – Blackbird 2010-11-09 11:28:03