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;
}
REVISED TO POST BELOW:
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++;
}
}?>
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.
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