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 am creating a Wordpress page where all my posts in the child categories of team are displayed in a grid. I got this working with the below code. The formatting and general layout is great however it only shows the 5 most recent posts in the child category instead of all of them. In my Wordpress account I have 7 posts which should be displaying. How should I retrieve the posts so it displays them all.

                <ul class="faces">

 <?php
$categories = get_categories( 'child_of=2' );  
foreach  ( $categories as $category ) {

    echo '<div class="grid-row"><h2>'.$category->name.'</h2></div>';

    $cat_posts = get_posts( 'cat='.$category->term_id );
    $end = count( $cat_posts ) - 1;
    $i = 0;
    foreach ( $cat_posts as $post ) {
        setup_postdata( $post );
        $face = get_field( 'face' );
        $name = get_field( 'fullname' );

        if ( $i % 6 === 0 ) {
            echo '<div class="grid-row">';
        }
      echo '<div class="obj">';

        echo wp_get_attachment_image($face)
            . '<div class="name">'.$name.'</div>';

        echo '</div>';
                if ( $i % 6 === 5 ) {
            echo '</div>';
        }
               $i++;
    }
}?>
    </ul>

//css

div.grid-row {
    width: 100%;

   height: 100%;
    position: relative;
    overflow: hidden;
}
div.obj{
float: left;
    position: relative;
    padding-right: 10px;
}
.faces{
    width: 1000px;

}

enter image description here

enter image description here

enter image description here

REVISED TO POST BELOW: enter image description here

REV FOR POST BELOW:

<?php
$categories = get_categories( 'child_of=2' );  
foreach  ( $categories as $category ) {

    echo '<div class="grid-row"><h2>'.$category->name.'</h2></div>';
    $args1 = array( 'posts_per_page' => -1, 'cat='.$category->term_id );
    $cat_posts = get_posts( $args1 );
    $end = count( $cat_posts ) - 1;
    $i = 0;
    foreach ( $cat_posts as $post ) {
    $post_category = get_the_category($post->ID);
    if($post_category->cat_name == $category->name){
        setup_postdata( $post );
        $face = get_field( 'face' );
        $name = get_field( 'fullname' );

        if ( $i % 6 === 0 ) {
            echo '<div class="grid-row">';
        }
      echo '<div class="obj">';

       echo '<div class="faceThumb">';
       echo wp_get_attachment_image($face);
       echo '</div>';
          echo '<div class="name">';
          echo $name;
          echo '</div>';

        echo '</div>';
                if ( $i % 6 === 5 ) {
            echo '</div>';
        }
        }
               $i++;

    }
}?>
share|improve this question
add comment

2 Answers

One of the arguments you can supply to get_posts is posts_per_page. If you don't include this, then it will default to your Pages Show at Most setting on the Reading page of your settings. http://wordpress.org/support/topic/get_posts-not-pull-all-posts-unless-numberposts-in-query. Yes, I know the forum thread is 2 years old, but it confirms the suspicion that I had after reading http://codex.wordpress.org/Template_Tags/get_posts and the Source File listed at the bottom of that page.

share|improve this answer
    
how would I add that into this line : $cat_posts = get_posts( 'cat='.$category->term_id ); –  BDGapps Jul 28 '13 at 7:40
    
Look at the example on the codex page. They create an array of arguments and then call $posts_array = get_posts( $args); In your case, it would be $cat_posts = get_posts($args); –  lonehorseend Jul 28 '13 at 7:53
    
if you look now it is getting all of them however not by child category. 2013 has 7 posts and 2012 only has 1 post. Why are both showing up in each? I added: $args = array( 'posts_per_page' => -1, 'cat='.$category->term_id ); $cat_posts = get_posts( $args ); –  BDGapps Jul 28 '13 at 12:56
    
Because you have to specify the parent category. I ran into this problem when I was trying to have category pages display only the categories directly underneath them and not their grandchildren. I wrote a snippet for it at rightthewriter.wordpress.com/2010/07/20/… –  lonehorseend Jul 29 '13 at 0:58
    
I looked through that and am a tad confused do you mind explaining how it works so i can figure out how to implement it. –  BDGapps Jul 29 '13 at 1:45
show 5 more comments

Can be a lot simpler. I don't know what get_field is, I think is your custom function... but why don't use standard custom fields and post thumbnail future? Anyway

$categories = get_categories( 'child_of=3' ); 

foreach  ( $categories as $category ) {
  $i = -1;
  echo '<div class="grid-row"><h2>' . $category->name . '</h2></div>';
  $args = array( 'posts_per_page' => -1, 'cat' => $category->term_id );
  $cat_posts = new WP_Query($args);
  if ( $cat_posts->have_posts() ) : while ( $cat_posts->have_posts() ) :
    $i++;
    $cat_posts->the_post();
    $face = get_field( 'face' );
    $name = get_field( 'fullname' );
    if ( $i % 6 == 0 ) echo '<div class="grid-row">';
    echo '<div class="obj">';
    echo '<div class="faceThumb">';
    echo wp_get_attachment_image($face);
    echo '</div><div class="name">' . $name . '</div></div>';
    if ( ($i % 6 == 5) || $i == ($cat_posts->post_count - 1) ) echo '</div>';
  endwhile; endif;
}
wp_reset_postdata();
share|improve this answer
    
it no longer shows the same for both so it works from that standpoint however it no longer wraps every 6th –  BDGapps Jul 31 '13 at 16:14
    
I've edited the code, now it should works. I've just changed $i = 0; in $i = -1; in the second line. –  G. M. Jul 31 '13 at 17:00
    
nope still getting 7 all in a row. –  BDGapps Aug 1 '13 at 0:22
    
Ok, now works. There was an error even in you code, when all posts were not a multiple of 6, last div of a row was not closed. Now it's all ok. This was not a WP related problem... however is solved. –  G. M. Aug 1 '13 at 2:52
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.