2016-08-22 111 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

相关问题