0

我有以下代码:

    <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 --> 

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

4

2 回答 2

1

so within your foreach loop you can do a separate query to get all the posts by current category ID

$args= array("category" => $cat->cat_ID);
$posts_in_category = get_posts($args);

and then loop through these results to output each post's title by accessing the title member variable

foreach($posts_in_category as $current_post) {
    echo $current_post->title;
}
于 2013-09-08T12:03:02.163 回答
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>';
于 2013-09-08T12:15:48.473 回答