I'm building a Wordpress theme for a client and it's unlike any other theme I've built before in that the homepage is a collection of posts spread across multiple different containers and styles. It's supposed to look and feel like a magazine in that, at the beginning of each month, 9 different posts are published simultaneously that each belong to a different category. And many of the posts are styled differently, I'm unable to use the traditional Loop:
<?php if(has_posts()) : while(has_posts)) : the_post(); ?>
// do stuff
<?php endwhile; endif; ?>
First, I'll explain how I'm trying to solve the problem, then describe the issues I'm having.
At the top of index.php, I have the following PHP code:
$slugPostMap = array();
$slugs = array(
'art', 'big-idea', 'books', 'educators', 'film',
'ministry-update', 'music', 'parents', 'students',
);
$args = array(
'limitposts' => 1,
'order' => 'date'
);
foreach($slugs as $slug){
$args['category_name'] = $slug;
$post = get_posts($args);
$slugPostMap[$slug] = $post;
}
The $slugs
array is a collection of each of the category slugs that I'll need to query with - each of these will have one post to be displayed. The idea is that I can loop over this array and, using an otherwise-fixed set of query parameters ($args
), retrieve the single most recent post for each of these slugs. As I'm looping over the slug array, I'm storing the query result in an associative array ($slugPostMap
) where the category slug "points to" the actual post.
Later on in the body of the page, I'm grabbing the required post from $slugPostMap
using the category slug index. Here's an example:
<?php
foreach($slugPostMap['ministry-update'] as $p):
setup_postdata($p);
?>
<a class="homepageVideoPostTitle" href="<?php the_permalink(); ?>"><?php the_date('F Y','','',true); ?></a>
<div class="homepageVideoPostSubtitle">
<?php the_title(); ?>
</div> <!-- end homepageVideoPostSubtitle -->
<div class="homepageVideoPostContent">
<?php the_excerpt(); ?>
</div>
<?php endforeach; ?>
The reason I'm using a foreach loop at the beginning is because, as I understand it, several of the core Wordpress template tags (the_title()
, the_permalink()
, etc.) are only available within The Loop. Because I'm calling setup_postdata()
manually here (which is called behind the scenes during The Loop), I'm also using the foreach to limit the scope of whatever weirdo voodoo happens when I call setup_postdata()
.
For the most part, this works. In the above example, the results of calling the_excerpt()
appear as expected. However, some other template tags aren't returning any values (the_permalink()
and the_title()
, specifically). I'm not getting any errors and the page loads fine when I try this out in a real live Wordpress install.
I'm assuming the problem lies in my lack of understanding of how to properly bypass The Loop in this situation, but the fact that there aren't any errors or any other clue as to the problem has brought me here.
This is Wordpress 3.0 running on some flavor of Linux. I've got many other Wordpress installs running on this box without issue and I'm able to use this same Wordpress instance with the default theme without a problem.
Thank you for your time - sorry for the novel. Please let me know if I there's any relevant details I've omitted.