2014-09-04 30 views
0

我真的很苦惱,我確信是一個簡單的問題。我似乎無法獲得屬於某個標記下的帖子以顯示在該標籤頁上,例如:(/ tag/blog /),博客是標籤。如何顯示具有特定標記的帖子

到目前爲止,Wordpress上的Official Tag Templates頁面仍然無法工作。

我不需要一個層次,所以tag.php工作正常。使用single_tag_title()它可以正確顯示頁面頂部的標籤。

Official Tag Templates的其餘部分沒有提供更多關於它的細節,我可以使用默認的Loop或自定義的Loop。我試圖用下面顯示的自定義之一,但不起作用。 (我已經把它降到最低目前不擔心造型。)

<?php get_header(); ?> 
<p>Tag: <?php single_tag_title(); ?></p> 
<div class="container"> 
    <div id="content" class="clearfix row"> 
     <div id="main" class="col col-lg-8 clearfix blog-page-padding" role="main"> 
      <?php 
       if (have_posts()) : 
        while (have_posts()) : 
         the_title(); 
        endwhile; // end while 
       endif; // end if 
      ?> 
     </div> <!-- end #main --> 
    <?php get_sidebar('blog'); // Blog ?> 
    </div> <!-- end #content --> 
</div> 
<?php get_footer(); ?> 

所以這個代碼目前並顯示標記的標題,但不是我正努力走出帖子的標題。

把問題包裝起來。 「我如何顯示屬於某個標籤的帖子。」

+0

是這些默認標籤,還是您使用自定義分類標籤作爲標籤 – 2014-09-04 16:31:33

+0

默認標籤。謝謝 – jackdh 2014-09-05 09:59:41

+0

你是否切換到了捆綁主題之一。 WordPress的V4今天發佈,你有升級。你沒有任何代碼限制來自特定用戶角色的訪問和信息,或者沒有登錄的用戶 – 2014-09-05 15:32:21

回答

0

我發現我不得不使用WP查詢循環,使他們由於被定製的帖子類型。

<?php if (is_tag()) {$term_id = get_query_var('tag_id'); $taxonomy = 'post_tag'; $args ='include=' . $term_id; $terms = get_terms($taxonomy, $args);} ?> 
      <!-- This gets the tags slug --> 

<?php $query = new WP_Query(array("post_type" => array('blog', 'portfolio'), "tag" => $terms[0]->slug )); while ($query->have_posts()) : $query->the_post(); ?> 

     <?php the_title(); ?> 
<?php endwhile; ?> 
+1

你應該在你的下一篇文章中添加**「details」**。另外,你可以使用'pre_get_posts'動作 – 2014-09-09 17:42:51

0

你不會告訴循環在任何地方尋找特定的標籤......你所做的只是在頁面頂部顯示當前選定的標籤。添加一個PHP if語句轉換成你的循環抓住與標籤的帖子:

<?php get_header(); ?>  // Get the header 

<p>Tag: <?php single_tag_title(); ?></p>  // Display the tag name 

<div class="container"> 
    <div id="content" class="clearfix row"> 
     <div id="main" class="col col-lg-8 clearfix blog-page-padding" role="main"> 


      <?php if (have_posts()) : while (have_posts()) : the_post(); ?> // Start your loop 

        <?php if(tag_slug(' THE SLUG ')) : ?> // Look for the tag 
          // Post HTML stuff 
        <?php endif; ?> 

       <?php endwhile; endif ?> // finish the loop 

     </div> <!-- end #main --> 
    <?php get_sidebar('blog'); // Blog ?> 
    </div> <!-- end #content --> 
</div> 
<?php get_footer(); ?> 

This page from the Codex有如何抓住類別和標籤的例子。

+0

感謝您的回覆,我試過使用您的標籤並用有問題的標籤替換SLUG,但它似乎並不想工作。有什麼你想讓我去測試,看看爲什麼? – jackdh 2014-09-04 15:50:43

+0

你在'if'循環中包含了post PHP,對嗎?我沒有在上面的示例中顯示它,所以添加'<?php the_content(); ?>看看是否有效。 – Brian 2014-09-04 15:57:23

+0

替換//用HTML發佈HTML內容?是的,我做了,但沒有發佈任何內容。這讓我覺得它仍然無法找到帖子。 – jackdh 2014-09-04 16:02:50

0

你只需要the_post()函數,你必須在while中調用它,它應該是你調用的第一個函數,因爲它加載了模板標籤。

<?php 
       if (have_posts()) : 
        while (have_posts()) : the_post(); 

         the_title(); 
        endwhile; // end while 
       endif; // end if 
      ?> 

如果標籤在自定義文章類型只是利用了你應該添加這樣的事情對你的functions.php:

add_action('pre_get_posts', 'custom_function'); 

function custom_function($query){ 
    if(! is_admin()){ 
    if(is_tag()){ 
     $query->set('post_type', array('your_post_type', 'your_other_post_type', 'post')); 
    } 
    } 

} 
+0

感謝您的回覆。雖然我恐怕沒有用。 – jackdh 2014-09-04 15:51:31

+0

你的標籤不是空的嗎?嘗試打開調試。您沒有任何自定義查詢或過濾器? – 2014-09-04 16:08:32

+0

感謝您的回覆。沒有標籤確實有一些帖子。該頁面上沒有自定義查詢或過濾器。我不太清楚如何使用調試。 – jackdh 2014-09-04 16:26:40

相關問題