2015-07-03 81 views
15

這個問題已更新。請參閱下面的最新版本Get_the_terms - 顯示所有帖子類型

我在使用自定義帖子時遇到了所有帖子類型的麻煩。這是基於同位素,並且用戶應該點擊鏈接來查看該類別內的帖子。

由Wordpress標準帖子創建的所有帖子都會出現,但沒有創建的任何類型(自定義帖子)。

<ul id="filters" class="whitetext whitelink myeluft"> 
    <li><a href="#" data-filter="*" class="selected">Alle</a></li> 
    <li><a href='#' data-filter='.foto'>Foto</a></li> 
    <li><a href='#' data-filter='.video'>Video</a></li> 
    <li><a href='#' data-filter='.web'>Web</a></li> 
</ul> 

<?php $the_query = new WP_Query('posts_per_page=50'); //Check the WP_Query docs to see how you can limit which posts to display ?> 
<?php if ($the_query->have_posts()) : ?> 
    <div id="isotope-list"> 
     <?php while ($the_query->have_posts()) : $the_query->the_post(); 


     // Query posts - post_types 
     $anypost = get_posts(array(
      'post_type'  => 'any' // every post type, but not attachments 
     )); 

     $termsArray = get_the_terms($post->ID, "category", $anypost); //Get the terms for this particular item 
     $termsString = ""; //initialize the string that will contain the terms 
      foreach ($termsArray as $term) { // for each term 
       $termsString .= $term->slug.' '; //create a string that has all the slugs 
      } 
     ?> 
     <div class="<?php echo $termsString; ?> item col-md-3"> <?php // 'item' is used as an identifier (see Setp 5, line 6) ?> 
      <h3><?php the_title(); ?></h3> 
       <?php if (has_post_thumbnail()) { 
          the_post_thumbnail(); 
        } ?> 
     </div> <!-- end item --> 
     <?php endwhile; ?> 
    </div> <!-- end isotope-list --> 
<?php endif; ?> 

正如你所看到的,我試圖通過插入下面的代碼來修復它,但它仍然不顯示所有的帖子類型。

// Query posts - post_types 
$anypost = get_posts(array(
    'post_type'  => 'any' // every post type, but not attachments 
)); 

$termsArray = get_the_terms($post->ID, "category", $anypost); //Get the terms for this particular item 

我已閱讀this article,但是我發現我自己更多的損失比開始/

會是怎樣一個可行的解決方案?

更新

通過使用下面的代碼,我能看到的所有信息,但不能夠將它們過濾掉。這裏參見頁:http://goo.gl/e3cLuM(向下滾動,直到你看到所有的職位)

<?php $post_type = 'any'; 
$post_taxonomy = 'any'; 
// Get all 
$terms = get_terms($post_taxonomy); 

$portfolio = new WP_Query('post_type='.$post_type.'&post_per_page=-1'); ?> 
// First we loop our porfolio_category to show all categories as filter. 
<ul id="filters" class="whitetext whitelink myeluft"> 
    <a href="#" data-filter="*" class="selected"><li class="smoothtrans">Alle</li></a> 
    <a href='#' data-filter='.foto'><li class="smoothtrans">Foto</li></a> 
    <a href='#' data-filter='.video'><li class="smoothtrans">Video</li></a> 
    <a href='#' data-filter='.web'><li class="smoothtrans">Web</li></a> 
</ul> 

<?php if ($portfolio->have_posts()) : ?> 
       <div id="isotope-list"> 
        <?php while ($portfolio->have_posts()) : $portfolio->the_post(); 
// Get current post terms. 
         $item_terms = wp_get_post_terms(get_the_ID(), $post_taxonomy, $args); 
         $classes = ''; 
         // Append classes to use with each item. 
         foreach($item_terms as $item_term){ 
          $classes .= $item_term->slug.' '; 
         } 
         ?> 
         <div class="<?php echo $termsString; ?> item col-md-4"> 
          <ul class="grid cs-style-3"> 
           <li> 
            <figure> 
             <?php // 'item' is used as an identifier (see Setp 5, line 6) ?> 
             <?php if (has_post_thumbnail()) { 
               the_post_thumbnail(); 
             } ?> 
             <figcaption class="lefttext"> 
              <h3><?php the_title(); ?></h3> 
              <span class="offgrey">Nettside</span> 
              <a href="#" class="smoothtrans">Se prosjekt</a> 
             </figcaption> 
            </figure> 
           </li> 
          </ul>    
         </div> <!-- end item --> 
        <?php endwhile; ?> 
       </div> <!-- end isotope-list --> 
      <?php endif; ?> 
+0

您是否在自定義帖子類型中使用了自定義分類標準? –

+0

感謝您的回答。這是一個負面的情況,但如果有必要使其發揮作用,則可以添加分類。 – Olen

+0

您是否嘗試過WP_Query –

回答

3

的問題已經解決。

我終於用下面的代碼:

<ul id="filters" class="whitetext whitelink myeluft"> 
     <a href="#" data-filter="*" class="selected"><li class="smoothtrans">Alle</li></a> 
     <a href='#' data-filter='.foto'><li class="smoothtrans">Foto</li></a> 
     <a href='#' data-filter='.video'><li class="smoothtrans">Video</li></a> 
     <a href='#' data-filter='.web'><li class="smoothtrans">Web</li></a> 
</ul> 

<?php 
$terms = get_terms($post_taxonomy); 

$args = array(
    'post_type' => 'any', 
    'posts_per_page' => 6, 
    'post_taxonomy' => 'any', 
); 

$the_query = new WP_Query($args); 


// Loop post_type 
?> 

<?php if ($the_query->have_posts()) : ?> 
    <div id="isotope-list"> 
     <?php while ($the_query->have_posts()) : $the_query->the_post(); 

     $termsArray = get_the_terms($post->ID, "category"); //Get the terms for this particular item 
     $termsString = ""; //initialize the string that will contain the terms 
      foreach ($termsArray as $term) { // for each term 
       $termsString .= $term->slug.' '; //create a string that has all the slugs 
      } 
     ?> 
     <div class="<?php echo $termsString; ?> item col-md-4"> 
      <ul class="grid cs-style-3"> 
       <li> 
        <figure> 
         <?php // 'item' is used as an identifier (see Setp 5, line 6) ?> 
         <?php if (has_post_thumbnail()) { 
           the_post_thumbnail(); 
         } ?> 
         <figcaption class="lefttext"> 
          <h3><?php the_title(); ?></h3> 
          <span class="offgrey"><?php echo(types_render_field("produkt", array('raw' => true))); ?>/<?php echo(types_render_field("produsert", array('raw' => true))); ?></span> 
          <a href="<?php the_permalink() ?>" rel="bookmark" class="smoothtrans">Se prosjekt</a> 
         </figcaption> 
        </figure> 
       </li> 
      </ul>    
     </div> <!-- end item --> 
     <?php endwhile; ?> 
    </div> <!-- end isotope-list --> 
    <script src="<?php bloginfo('stylesheet_directory'); ?>/js/toucheffects.js"></script> 
<?php endif; ?> 

有沒有那麼多的變化,我不得不這樣做,但有幾個。

感謝所有評論並帶有工作理念的人。

請注意,我還沒有修復代碼中的一些錯誤。

0

any參數是post_status,我不認爲這是接受post_type'post_status' => 'any') (實際上,這不是在一些版本)

你可以給post_type陣列與一些類型,而不是正確地實現:

'post_type' => array('post','page','event') 
+0

感謝您的評論。我已經測試過它,但似乎沒有工作。我也嘗試用自定義後置slu replace替換數組,但這也不起作用... – Olen

+0

然後檢查數據庫,看看是什麼使這些帖子不同(類別等)。 – FeedTheWeb

+0

我已經更新了這個問題。如果你喜歡,請看原始問題。 – Olen

4

假設我們有自定義後類型,稱爲portfolio和自定義分類稱爲portfolio_category

<?php $post_type = 'portfolio'; 
$post_taxonomy = 'portfolio_category'; 
//First get all terms of portfolio_category. 
$terms = get_terms($post_taxonomy); 

$portfolio = new WP_Query('post_type='.$post_type.'&post_per_page=-1'); ?> 
// First we loop our porfolio_category to show all categories as filter. 
<ul id="filters" class="whitetext whitelink myeluft"> 
    <li><a href="#" data-filter="*" class="selected">All</a></li> 
    <?php foreach($terms as $term) : ?> 
     <li><a href='#' data-filter='.<?php echo $term->slug ?>'><?php echo $term->name ?></a></li> 
    <?php endforeach; ?> 
</ul> 

<?php if ($portfolio->have_posts()) : ?> 
       <div id="isotope-list"> 
        <?php while ($portfolio->have_posts()) : $portfolio->the_post(); 
// Get current post terms. 
         $item_terms = wp_get_post_terms(get_the_ID(), $post_taxonomy, $args); 
         $classes = ''; 
         // Append classes to use with each item. 
         foreach($item_terms as $item_term){ 
          $classes .= $item_term->slug.' '; 
         } 
         ?> 
         <div class="<?php echo $classes; ?> item col-md-3"> 
          <h3><?php the_title(); ?></h3> 
          <?php if (has_post_thumbnail()) { 
           the_post_thumbnail(); 
          } ?> 
         </div> <!-- end item --> 
        <?php endwhile; ?> 
       </div> <!-- end isotope-list --> 
      <?php endif; ?> 
+0

謝謝。我相信我們在這裏有所收穫。我將post_taxonomy添加到自定義文章中的帖子:Foto,並且它出現了。我用foto_category作爲分類。但是,要顯示名稱的foreach中的列表無法正常工作。該頁面可以在這裏找到:http://goo.gl/jytG7o(向下滾動,直到看到Siste prosjekt)。 – Olen

+0

對不起,我錯了。我的意思是我用照片類別作爲slu,,用照片作爲分類。 – Olen

+0

我已更新該問題。如果你喜歡,請看原始問題。 – Olen

相關問題