2010-11-09 82 views
0

查看標籤頁時,是否可以以某種方式輸出(在循環內)標籤所在的類別而不是列出其帖子?顯示標籤類別

在我的主頁循環看起來如下,即時尋找相同的東西,但在標籤頁上。

  <ul class="thumb-grid"> 
      <?php query_posts('meta_key=is_front_page&posts_per_page=5&paged=$paged'); ?> 
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
       <li id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
        <a href="<?php bloginfo('url'); ?>/movies/<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>"> 
         <img src="<?php echo $my_image_url = get('movie_thumbnail'); ?>" alt="" /> 
         <span class="title"><?php $category = get_the_category(); echo $category[0]->cat_name; ?></span> 
         <span class="feat"><?php $articletags = strip_tags(get_the_tag_list('',', ',''));echo $articletags;?></span> 
        </a> 
       </li> 
      <?php endwhile; endif; ?> 
      <?php wp_reset_query(); ?> 
      </ul> 
+0

這將是一個自定義查詢。由於標籤和類別只與它們共享的帖子間接相關,因此您必須循環所有帖子(或創建適當的SQL連接)才能找到所有組合。你設想什麼樣的用戶界面? – 2010-11-09 11:03:39

+0

嗨,我添加了我的HP循環的外觀,我試圖得到的標籤頁循環應該也是類似的,顯然只輸出標籤所在的類別。 – Blackbird 2010-11-09 11:26:28

+0

想想如果通過查詢meta的更簡單的方法鍵「is_front_page」並檢查標籤是否存在於任何這些帖子中?我不知道這一切是否可能。 – Blackbird 2010-11-09 11:28:03

回答

0

這是一個解決方案,用於輸出包含在頁面上共享當前標籤一次的帖子的類別列表,而不是在循環內。我只在標記頁面上測試它,其中'標記'查詢變量可用。它使用自定義查詢using the $wpdb class。 (這對學習非常有用)。

您需要在開頭定義自己的表格前綴,或設置爲''

測試 我測試了這個代碼,直接把它放在tag.php中。將它放在你自己的插件或主題的functions.php中會更聰明。一些調整可能是必要的,以確保$ wp_query對象在該函數內可用。

術語和分類標準 請記住,標籤和類別在wordpress數據庫中被認爲是相等的。與帖子,頁面,修訂和草稿一樣,所有「帖子」,類別和標籤都是「條款」。它們之間的區別在於它們的「分類法」,這就是該術語是「類別」還是「標籤」。

「條款」表包含所有標籤和類別。 'term_taxonomy'存儲該術語是標籤,類別還是帖子鏈接。 'term_relationships'表對帖子和分類進行配對。 'object_id'列可以保存該表中的發帖ID或術語ID。

代碼大綱

  1. 設置表前綴
  2. 獲取從query_vars
  3. 從術語表
  4. 獲取標籤的term_id標籤名稱獲取的帖子ID的(命名爲「OBJECT_ID ')從term_relationships表中作爲數字索引數組
  5. 將該數值數組轉換爲以逗號分隔的字符串
  6. 飾件後面的逗號
  7. 選擇具有從#4的帖子ID的從term_relationships作爲多維關聯數組
  8. 創建所有的職位具有該標籤的術語的陣列中的所有term_taxonomy_id的(類別和標記) 。
  9. Foreach元素在這個數組中,選擇分類法(無論是'category'還是'tag'),如果它是一個類別,則將term_id放入數組中。
    1. 將該數組轉換爲字符串以供下一步使用。
    2. 呼叫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>'; 
} 
+0

欣賞他們的伴侶的努力,似乎工作,但不是我所期待的。我希望能夠通過循環輸出貓的標籤功能,以便我可以使用貓的描述和元數據對每個樣式進行樣式設置。 – Blackbird 2010-11-10 13:47:54

+0

這個問題可能比我想象的更復雜,可能會尋求更簡化的佈局。 – Blackbird 2010-11-10 13:48:46

+0

查看可能解決您評論的更新 – kevtrout 2010-11-10 13:55:33