Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have put together this loop to display all children posts of the desired cat, but i am not getting any of the posts from one of the children cats..
Not sure if i am using the best method to accomplish what I am trying to do..

 <?php
        $posts = array();
         $categories = get_categories('child_of=5');
         foreach($categories as $category) {
           $args=array(
            'post_per_page' => 5,
            'orderby' => 'post_date',
            'numberposts' => 45,
            'category__in' => array($category->term_id)             
           );
         $posts = $posts + get_posts($args);
        } // Close your foreach here
             shuffle($posts);
              if ($posts) {
                foreach($posts as $post) {
                  setup_postdata($post);
                   ?>
                    <div <?php post_class('boxy');?>><div  class="soc-label" ></div>

                  <?php 
                      if ( has_post_thumbnail()) {
                      $full_image_url =    wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
                      echo '<a href="' . $full_image_url[0] . '" rel="lightbox" title="' . the_title_attribute('echo=0') . '" >';
                      the_post_thumbnail('thumbnail');
                      echo '</a>';
                    }
                   ?>

                    <?php the_content(''); ?>
                    </div>


        <?php } 
           } 

        ?>

I am using the results in a tiled mosaic of sorts with Isotope, and I am shuffling the results, thats also a must.. Any guidance would be appreciated I have spend the last couple weeks getting as far as I am now.

share|improve this question
    
Do you mean to have posts_per_page set to 5? –  mrwweb Mar 1 '13 at 0:41
    
I've been trying many things to get the desired results.. I am looking to have the most recent 5 posts from each child cat. –  StephenPerrett Mar 1 '13 at 1:03
    
Is it just one category that isn't working? If so, does it happen to be a "grandchild" category? –  mrwweb Mar 1 '13 at 1:10
    
yes just one cat, and no its a child not a grandchild.. –  StephenPerrett Mar 2 '13 at 0:15
add comment

2 Answers

up vote 1 down vote accepted

Your foreach is way wrong. Try combining the category ids in an array, then running one query only to get the posts you want.

Edit: read your comments. Try use using an array function like array_merge instead of the + to combine the resulting post arrays. See if that helps.

share|improve this answer
    
Otto, thanks for the input. I am a beginner/novice at best with PHP.. I have put the code here together from research, not sure how to put the logic I'm trying to achieve together correctly.. –  StephenPerrett Mar 2 '13 at 0:25
    
Otto, I made a couple small changes with your input and was able to get the result I was looking for, Thank you! ' –  StephenPerrett Mar 2 '13 at 6:23
add comment

I Was able to get it working using array_merge().. Here is the code.. still might have a couple code errors but its working.

    <?php
        $posts = array();
         $categories = get_categories('child_of=4');
         foreach($categories as $category) {
           $args=array(         
            'orderby' => 'post_date',
            'numberposts' => 40,
            'category__in' => array($category->term_id)             
           );
         $posts = array_merge($posts,get_posts($args));
        } // Close your foreach here
             shuffle($posts);
              if ($posts) {
                foreach($posts as $post) {
                  setup_postdata($post);
                   ?>
                    <div <?php post_class('boxy');?>><div class="soc-label" ></div>

                  <?php 
                      if ( has_post_thumbnail()) {
                      $full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
                      echo '<a href="' . $full_image_url[0] . '" rel="lightbox" title="' . the_title_attribute('echo=0') . '" >';
                      the_post_thumbnail('thumbnail');
                      echo '</a>';
                    }
                   ?>

                    <?php the_content(''); ?>
                    </div>


        <?php } 
           } 

        ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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