I've been building a WordPress template based on inuitcss to get a responsive page.
In this template, I'm attempting to get the WP posts from a certain category, and divide them over a predefined amount of columns. To achieve this I'm counting the amount of posts from category and compute the necessary data for the posts to split up evenly.
I've been using this (last on the page) example and updated several deprecated functions.
The issue I am facing at the moment is that all columns are loading the same posts, and they are only loading the last two instead of all available posts in the category.
The code below is what I am using at the moment, the result can be found at http://www.alikivanderkruijs.com/wp (click on 'Projects').
I've been sitting on this for a while and can't seem to get it right. I hope someone can help me out!
<?php
$proj_posts = get_posts('cat=15');
$count_proj_posts = count($proj_posts);
$split_proj_posts = round($count_proj_posts/2);
?>
<div class="gw">
<div id="menuwrap">
<h3 class="work">PROJECTS</h3>
<div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php query_posts('cat=15&showposts=' . $split_proj_posts . ''); ?>
<?php $posts = get_posts('posts_per_page=' . $split_proj_posts . '&offset=0'); foreach ($posts as $post) : the_post(); ?>
<?php static $proj_count1 = 0; if ($proj_count1 == $split_proj_posts) { break; } else { ?>
<div <?php post_class(); ?>>
<h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
<?php the_content(); ?>
</div>
<?php $proj_count1++; } ?>
<?php endforeach; ?>
</div>
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php rewind_posts(); ?>
<?php query_posts('cat=15&showposts=' . $split_proj_posts . ''); ?>
<?php $posts = get_posts('posts_per_page=' . $split_proj_posts . '&offset=' . $split_proj_posts . ''); foreach ($posts as $post) : the_post(); ?>
<?php static $proj_count2 = 0; if ($proj_count2 == $split_proj_posts) { break; } else { ?>
<div <?php post_class(); ?>>
<h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
<?php the_content(); ?>
</div>
<?php $proj_count2++; } ?>
<?php endforeach; ?>
</div>
</div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
</div>
You are overcomplicating it, simply get all the posts and count up until half, then echo out the closing and opening tags to create a new row:
<div class="gw">
<div id="menuwrap">
<h3 class="work">PROJECTS</h3>
<div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php
$args = array(
'numberposts' => -1,
'posts_per_page' => '',
'offset' => 0,
'category' => '15',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true);
$posts_array = get_posts($args);
$split_at = round(count($posts_array)/2)-1;
$count = 0;
foreach ($posts_array as $post_object):setup_postdata($post);
?>
<div <?php post_class(); ?>>
<h4><a href="<?php the_permalink() ?>" rel="bookmark"
title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h4>
<?php the_content(); ?>
</div>
<?php
//IF we are halfway through, close 1st row and
start a new one
if ($count == $split_at) {
?>
</div>
</div>
<div class="g two-sixths lap-two-sixths palm-one-whole">
<div class="content">
<?php
}
$count++; ?>
<?
endforeach;
?>
</div>
</div>
<div class="g one-sixth lap-one-sixth nomob">
</div>
</div>
</div>
I have left the full args array, incase it is usefull to others, but you only need to use the cat argument in your case.
EDIT ok, setup_postdata is flawed, just checked the docs and it doesnt work as expected. The suggested workaround is to set the global post object like so:
setup_postdata( $GLOBALS['post'] =& $post_object )
Change that section should fix that issue. The count problem is still weird though.