I'm trying to find to find a way to create a loop that queries all categories and then the posts under those categories. I am using a custom loop to display my posts and I'd like to have it follow that same loop. The loop I am using is:

    if(have_posts()) :

    // Get the initial array with random numbers
    $randomArray = getRandomNumbers();

    // Open the initial row.
    echo '<div class="row">';

        // CHECK FOR POSTS
        while (have_posts()) : the_post();

            // CREATE ROWS

            if (count($randomArray) < 1) {
                // Close the row and start a new one:
                echo '</div><div class="row">';
                // Get a fresh array with random numbers:
                $randomArray = getRandomNumbers();

            $nextRandomNumber = array_pop($randomArray); // This takes the next number.

            // CREATE RANDOM COLUMN 
            echo '<div class="col-md-' . $nextRandomNumber . ' col-lg-' . $nextRandomNumber . ' post clearfix"><article class="entry-content">';

            // FETCH THUMBNAIL
            echo "<figure><a href='" . get_permalink() . "'>";
            if ( $nextRandomNumber == "6" ) {
                the_post_thumbnail('medium', array('class' => 'img-responsive pull-left')); 
            elseif ( $nextRandomNumber == "8" ) {
                the_post_thumbnail('thumb-image', array('class' => 'img-responsive pull-right')); 
            elseif ( $nextRandomNumber == "3" ) {
                the_post_thumbnail('medium', array('class' => 'img-responsive')); 
            elseif ( $nextRandomNumber == "4" ) {
                the_post_thumbnail('medium', array('class' => 'img-responsive')); 
            echo "</a></figure>";

            // FETCH TITLE
            echo "<h2><a href='" . get_permalink() . "'>" . get_the_title() . "</a></h2>";

            echo "<span>Published " . get_the_date() . "</span>";

            // FETCH POST'S CATEGORY
            foreach((get_the_category()) as $category) { echo "<h6>In <a href='/wordpress/category/". $category->cat_name . ' ' . "'>" . $category->cat_name . ' '."</a></h6>"; };
            // echo "<h6>In " . $category[0]->cat_name . "</h6>";

            // FETCH CONTENT
            echo ' <div class="entry-meta clearfix"><a href="' . get_author_posts_url(get_the_author_meta( 'ID' )) .'" class="topic-tag">'. get_the_author_meta( 'display_name' ) .'</a><nav><a href="#" data-toggle="popover" data-trigger="hover" title="Popover title" data-content="And here is some amazing content. It is very engaging. Right?"><i class="fa fa-paper-plane-o"></i></a><a href="#" rel="tooltip" data-original-title="Like"><i class="fa fa-heart-o"></i></a> <a href="#" rel="tooltip" data-original-title="Add to favorites"><i class="fa fa-plus"></i></a></nav></div></article></div>';

            // END

        // Close the final row.
        echo '</div>';


I found a post that does pretty much what I want: Get all categories and posts in those categories. I'm just unsure how I go about adding in my custom loop into the loop to get all the categories and it's post. How I'd like to queries the posts is like so:

[query each category]
<div class="bb-item">
    <div class="content">
        <div class="container scroller">
            [display custom loop for posts]
[end query for each category]

I'm still fairly new to PHP and WordPress theming so any advice would be greatly appreciated. Thanks!

  • I figured out to get each section for the categories the way I want it but I'm still unsure on how to add in my custom loop for the posts. Any help is still greatly appreciated. Thanks! Mar 16, 2015 at 15:19

1 Answer 1


Figured out a solution. Ended up querying each categories using "foreach" and inserting my custom function into the loop. Not sure if it's the best approach but I got it to work so eh. No one else seems to want to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.