显示子类别的帖子

时间:2013-09-08 10:34:21

标签: php wordpress

我有以下代码:

    <div id="content">
    <?php 
    $args = array(
        'orderby' => 'id',
        'hide_empty'=> 0,
        'child_of' => 10, //Child From Boxes Category 
    );
    $categories = get_categories($args);
    foreach ($categories as $cat) {
        echo '<div class="one_fourth">';
        echo '<h1 class="valignmiddle uppercase title-bold">'.$cat->name.'<img src="'.$cat->term_icon.'" alt=""  class="alignleft"/>'.'<br />'.'<span class="solutions">'.$cat->description.'</span>'.'</h1>';
        echo '<br />';
        echo '<span>';
        //How do I get these child post titles here 
        echo '</span>';
        echo '</div>';
    }
    ?>
    <div class="clear"></div>

    <hr />
    <div class="clear"></div>
</div><!-- #content --> 

我正在使用它从父框类别中获取类别,并显示子类别中的名称和图标。如何显示子类别的帖子标题?

2 个答案:

答案 0 :(得分:1)

因此,在您的foreach循环中,您可以执行单独的查询以按当前类别ID

获取所有帖子
$args= array("category" => $cat->cat_ID);
$posts_in_category = get_posts($args);

然后循环浏览这些结果,通过访问标题成员变量

输出每个帖子的标题
foreach($posts_in_category as $current_post) {
    echo $current_post->title;
}

答案 1 :(得分:0)

您可以使用get_posts作为要查询的类别$cat->term_id。我在functions.php中创建了一个函数:

function the_posts_titles_so_18682717( $cat_id )
{
    $args = array(
        'posts_per_page'   => -1,
        'category'         => $cat_id,
        'orderby'          => 'post_date',
        'order'            => 'DESC',
        'post_type'        => 'post',
        'post_status'      => 'publish',
    );
    $the_posts = get_posts( $args );
    if( $the_posts )
    {
        foreach( $the_posts as $post )
        {
            printf(
                '<h4><a href="%s">%s</a>',
                get_permalink( $post->ID ),
                $post->post_title
            );
        }
    }
}

并在您需要子标题的模板中调用它:

echo '<span>';
//How do I get these child post titles here 
the_posts_titles_so_18682717( $cat->term_id );
echo '</span>';