This should work, but it’s not tested
<div id="tabs">
<ul id="tab-menu">
<?php
$terms = get_terms('category');
global $wpdb;
$posts = array();
if ( is_array($terms) && ! empty($terms) ) {
$active = null;
foreach ( $terms as $i => $term ) {
$ids = $wpdb->get_col( $wpdb->prepare("SELECT object_id FROM $wpdb->term_relationships
WHERE term_taxonomy_id = %d",
$term->term_taxonomy_id
));
$articles_count = count($ids);
if ( $articles_count > 0 ) {
if ( is_null($active) ) $active = $i;
$posts[$term->term_id] = get_posts( array('post__in' => $ids, 'posts_per_page' => 5, 'orderby' => 'post_date', 'order' => 'DESC') );
}
$class = ($active == $i) ? 'term-heading active' : 'term-heading';
?>
<li class="<?php echo $class; ?>">
<a class="term-heading" href="#<?php echo $term->slug; ?>"><?php echo $term->name; ?></a>
</li>
<?php
}
echo '</ul> <!-- end #tab-menu -->' . PHP_EOL;
}
//This part of code creates tabs content
if ( is_null($active) ) $active = 0;
foreach ( $terms as $i => $term ) {
$class = ($active == $i) ? 'tab-content active' : 'tab-content';
?>
<div class="<?php echo $class; ?>" id="<?php echo $term->slug; ?>">
<?php
if ( ! empty($posts[$term->term_id]) ) {
echo '<ul>' . PHP_EOL;
global $post;
foreach ($posts[$term->term_id] as $post) {
setup_postdata($post);
?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>' . PHP_EOL;
} else {
printf('<p>%s</p>', __('No posts in this category.'));
}
wp_reset_postdata();
?>
</div>
<?php } ?>
</div> <!-- end #tabs -->