You get all categories with:
$categories = get_categories( array ( 'fields' => 'ids' ) );
This returns an array with all categories.
Then you should use get_posts()
or a new WP_Query
, because query_posts()
has many side-effects and should not be used anymore.
Sample code, as illustration:
$categories = get_categories( array ( 'number' => 5 ) );
print '<pre>$categories=" . htmlspecialchars( var_export( $categories, TRUE ), ENT_QUOTES, "utf-8', FALSE ) . '</pre>';
$boxes = array();
foreach ( $categories as $category )
{
$boxes[ $category->term_taxonomy_id ]['posts'] = get_posts(
array(
'numberposts' => 5,
'category' => $category->term_taxonomy_id,
'posts_per_page' => 3,
'post_type' => 'post'
)
);
$boxes[ $category->term_taxonomy_id ]['title'] = $category->category_nicename;
}
foreach ( $boxes as $cat_id => $box )
{
printf(
'<h2><a href="https://wordpress.stackexchange.com/questions/100749/%1$s">%2$s</a></h2>',
get_category_link( $cat_id ),
$box['title']
);
print '<ul>';
foreach ( $box['posts'] as $post )
printf(
'<li><a href="https://wordpress.stackexchange.com/questions/100749/%1$s">%2$s</a></li>',
get_permalink( $post->ID ),
esc_html( $post->post_title )
);
print '</ul>';
}