2016-08-22 114 views
0

嗨我有一個自定義帖子類型的問題。如何使用自定義帖子類型列出來自類別的帖子? (WP)

所以我想要做的是,在調用主類別時列出子類別的所有帖子。 Usualy這個曾與來自WordPress的正常型後沒有問題,但因爲我試圖用自定義後類型它不工作...

我的品類結構是這樣的:

  • 類別
    • 子 類別(帖子內部)
    • 子 類別(帖子內部)

任何幫助或提示表示讚賞。由於

<?php 
    $categories = get_categories('title_li=&hide_empty=1&parent=1430'); 

    foreach($categories as $category) { 
    echo "<div class='col-12' style='border-bottom: 0'><h1 class=''>".$category->name."</h1></div>"; 
    $args = array('cat'=> $category->term_id); 
    if (have_posts()) : while (have_posts()) : the_post(); ?> 
     <!-- article --> 
     <article class="col-3"> 
      <div class="image"> 
       <span class="helper"></span><a href="javascript:void(0)"><?php the_post_thumbnail('full');?></a> 
      </div> 
      <h1><a href="javascript:void(0)"><?php the_title(); ?></a></h1> 
      <?php the_content();?> 
     </article> 
     <!-- /article --> 
    <?php endwhile; endif; }?> 
    </main> 

回答

1

有幾個問題怎麼回事:

首先,你不聲明循環,或調用get_posts。其次,如果您查看WP_Query(這是get_posts後面的「主幹」,因此參數基本相同)的文檔,您會看到如果您不傳入參數post type,默認爲post

所以,既然你不跟我們分享的信息類型,你必須調整以下需要:

// .. your code above .... 

$args = array(
    'cat'=> $category->term_id, 
    // Include the post_type in the query arguments 
    'post_type' => 'custom-post-type' // Change this as needed 
); 

// Now we need to actually query for the posts... 
$custom_posts = new WP_Query($args); 
// These are modified to use our custom loop... 
if ($custom_posts->have_posts()) : while ($custom_posts->have_posts()) : $custom_posts->the_post(); ?> 
// .. your code below ... the_title(), etc will work here... 
+0

感謝,崗位類型數組,這是最缺少的東西,我曾嘗試添加$ custom_post但之前沒有工作。 – Darko

相關問題