Multiple Loops in Navigation With Get_Posts
-
I’ve got a dynamic fly-out menu setup for my navigation, and I’m using get_posts to pull in posts in each category. Unfortunately, it’s thrown off my main loop and now it’s trying to load the last post from the navigation for every post (there’s no post under that category yet).
Don’t have too much experience running multiple loops, so this may well be a simple fix that I’ve overlooking.
Here’s the navigation code:
<ul id="topnav"> <li><a href="/category/features/" class="features">Features</a> <div class="sub"> <ul> <li><h2><a href="/category/features/culture/">Culture</a></h2></li> <?php global $post; $tmp_post = $post; $myposts = get_posts('numberposts=2&offset=0&category=588'); foreach($myposts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> <ul> <li><h2><a href="/category/features/music/">Music</a></h2></li> <?php global $post; $myposts = get_posts('numberposts=2&offset=0&category=586'); foreach($myposts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> <ul> <li><h2><a href="/category/features/fashion/">Fashion</a></h2></li> <?php global $post; $myposts = get_posts('numberposts=2&offset=0&category=587'); foreach($myposts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> <ul> <li><h2><a href="/category/features/photographs/">Photos</a></h2></li> <?php global $post; $myposts = get_posts('numberposts=2&offset=0&category=605'); foreach($myposts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> </div> </li> <li> <a href="#" class="news">News</a> <div class="sub"> <ul> <?php global $post; $myposts = get_posts('numberposts=4&offset=0&category=196'); foreach($myposts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> </div> </li> <li><a href="/category/reviews/" class="reviews">Reviews</a> <div class="sub"> <ul> <?php global $post; $myposts = get_posts('numberposts=4&offset=0&category=198'); foreach($myposts as $post) : setup_postdata($post); ?> <li><?php the_post_thumbnail('review-cover-front'); ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <div class="Clearer"></div> <?php endforeach; ?> </ul> </div> </li> <li><a href="/category/columns/" class="columns">Columns</a> <div class="sub"> <ul> <li><h2><a href="/category/columns/pro-logic/">Pro Logic With Willie Green</a></h2></li> <?php global $post; $myposts = get_posts('numberposts=1&offset=0&category=775'); foreach($myposts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> <div class="row"> <ul> <li><h2><a href="/category/columns/first5/">Get Familiar</a></h2></li> <?php global $post; $myposts = get_posts('numberposts=1&offset=0&category=439'); foreach($myposts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> </div> <div class="row"> <ul> <li><h2><a href="/category/columns/geek-week/">Geek Thug: Technology Weekly</a></h2></li> <?php global $post; $myposts = get_posts('numberposts=1&offset=0&category=936'); foreach($myposts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> </div> </div> </li> <li><a href="/category/blogs/" class="blogs">Blogs</a> <div class="sub"> <ul> <li><h2><a href="/category/blogs/letter-from-the-editor/">Letter From the Editor</a></h2></li> <?php global $post; $myposts = get_posts('numberposts=1&offset=0&category=776'); foreach($myposts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> <div class="row"> <ul> <li><h2><a href="/category/blogs/psalm-one-blogs/">Psalm One Loves You More</a></h2></li> <?php global $post; $myposts = get_posts('numberposts=1&offset=0&category=997'); foreach($myposts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> </div> <div class="row"> <ul> <li><h2><a href="/category/blogs/Words-I-Manifest/">Words I Manifest</a></h2></li> <?php global $post; $myposts = get_posts('numberposts=1&offset=0&category=1337'); foreach($myposts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> <?php $post = $tmp_post; ?> </ul> </div> </div> </li> <li><a href="/category/the-leak/" class="music">Music</a> <div class="sub"> <ul> <li><a href="/category/the-leak/singles/">Singles</a></li> <li><a href="/category/the-leak/mixtapes/">Mixtapes</a></li> <li><a href="/category/the-leak/albums/">Albums</a></li> <li><a href="/category/the-leak/eps/">EPs</a></li> </ul> </div> </li> <li><a href="/forums/" class="forum">Forum</a></li> </ul>
and here’s the page code, for a news post:
<?php/*Template Name: TestNews*/?> <?php include('headernew.php') ?> <div id="maincontent"> <div id="breadcrumbs"> <?php if ( function_exists('yoast_breadcrumb') ) { yoast_breadcrumb('','');} ?> </div> <div id="pageleft"> <div id="adleft"> <?php include('categorylist.php'); ?> <script type="text/javascript"> GA_googleFillSlot("sideBanner"); </script> </div><div id="postcontent"> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <div id="title"> <h1><?php the_title(); ?></h1><div class="postinfo">Published by <?php the_author(); ?> on <?php the_time('F j, Y'); ?> at <?php the_time('g:i a'); ?> </div> </div> <div id="tweetme"><script type="text/javascript" src="https://tweetmeme.com/i/scripts/button.js"></script></div> <div class="clearer"></div> <?php the_content('<p>Read the rest of this page »</p>'); ?><span class="orangehighlight">Don't miss these:</span><div class="dontmiss"><ol><?php related_posts(); ?></ol></div> <div class="clearer"></div> <?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?> <?php comments_template('/comments_small.php') ?> <?php if (function_exists('akpc_most_popular_in_cat')) { akpc_most_popular_in_cat(); } ?> <?php endwhile; ?> <?php else : ?> <h2>Not Found</h2> <p>Sorry, but you are looking for something that isn't here.</p> <?php include (TEMPLATEPATH . "/searchform.php"); ?> <?php endif; ?> </div> </div> <?php include('sidebar-news.php'); ?> <div id="clearer"></div></div><?php include('footernew.php') ?>
I feel like the issue is a fairly obvious one with the way I’ve set up my main loop, but I can’t for the life of me figure out what I need to change to correct it.
I need help ASAP so I can get my site (https://abovegroundmagazine.com) back into working order.
Thanks!
Tyler
- The topic ‘Multiple Loops in Navigation With Get_Posts’ is closed to new replies.