Reputation: 840
I have the following code:
<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 -->
I'm using it to get categories from parent Boxes category and display Name and Icon from child categories. How to display posts titles from child categories?
Upvotes: 0
Views: 883
Reputation: 2456
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;
}
Upvotes: 1
Reputation: 26055
You can use get_posts
for that passing $cat->term_id
as the category to query. I'd create a function in 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
);
}
}
}
And call it in the point of the template you need the child titles:
echo '<span>';
//How do I get these child post titles here
the_posts_titles_so_18682717( $cat->term_id );
echo '</span>';
Upvotes: 0