WordPress post navigation with customized post loop using meta_key
What’s that?
I wrote this stupid navigation workout because I can’t found any of good practice on writing customized content unless I go for WordPress 3.0. When I create several content handling in a themes, such as photo post or video post, I was relied on the meta_key value to determine which style to be used.
For each single post or post loop, it’s no problem at all, however when i tried to do navigation on the single.php. I noticed that there is no way to treat them as typical blog post.
<?php
$category = get_the_category();
$catID = $category[0]->cat_ID;
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content();?>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
<!-- Start navigation here -->
<?php
global $post;
$mypostlist = array();
$currentPID = $post->ID;
$myNav[] += $currentPID; //insert the current post into first array position
$myposts = get_posts('exclude='.$post->ID.'&orderby=date&order=desc&numberposts=-1&meta_key=my_post_type&meta_value=1&offset=1&category='. $catID .'');
setup_postdata($post); // exclude current post from the loop
foreach ($myposts as $post) {
$myNav[] += $post->ID; // insert the result into the loop
}
$current = array_search($post->ID, $myNav);
$prevID = $myNav[$current-1];
$nextID = $myNav[$current++];
?>
<div class="navigation">
<?php if (!empty($prevID)) { ?>
<div class="alignleft"><a href="<?php echo get_permalink($prevID); ?>"
title="<?php echo get_the_title($prevID); ?>">Previous</a>
</div>
<?php }
if (!empty($nextID)) { ?>
<div class="alignright"><a href="<?php echo get_permalink($nextID); ?>"
title="<?php echo get_the_title($nextID); ?>">Next</a></div>
<?php } ?>
</div><!-- .navigation -->
</div>