Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $parent_cat = get_term_by('name', 'Blog', 'category');
- $child_cats = get_categories('child_of='.$parent_cat->term_id); //set the parent category
- $child_cats_ids = array();
- $counter = 0;
- if( $child_cats ) foreach( $child_cats as $cat ) { $child_cats_ids[] = $cat->term_id; } //get array of child cat IDs
- $child_cat_posts = get_posts( array( 'category__in' => $child_cats_ids, 'posts_per_page' => -1 ) );
- if( $child_cat_posts ) foreach( $child_cat_posts as $post ) {
- foreach( get_the_category($child_cat_post->ID) as $post_cat ) {
- if( in_array( $post_cat->term_id, $child_cats_ids ) ) {
- setup_postdata($post);
- //start output of post//
- the_title; echo ' - '; the_date(); echo ' - ' . $post_cat->name; echo '<br />';
- //end of post output//
- $key = array_search($post_cat->term_id,$child_cats_ids);
- unset($child_cats_ids[$key]);
- $counter++;
- break;
- }
- if( $counter >= 6 ) break; //max number of posts
- }
- wp_reset_postdata();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment