WordPress.org

Ready to get started?Download WordPress

Forums

Multiple Loops in Navigation With Get_Posts (7 posts)

  1. abovegrnd
    Member
    Posted 3 years ago #

    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="http://tweetmeme.com/i/scripts/button.js"></script></div>
                                    <div class="clearer"></div>
    				<?php the_content('<p>Read the rest of this page &raquo;</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 (http://abovegroundmagazine.com) back into working order.

    Thanks!

    Tyler

  2. Mark / t31os
    Moderator
    Posted 3 years ago #

    After you've finished looping over all your get_posts calls put a reset in..

    <?php wp_reset_query(); ?>

    That should restore the original query..

  3. abovegrnd
    Member
    Posted 3 years ago #

    Mark, thanks for the help. I put that in, but it's still doing the same thing unfortunately. New code is as follows:

    <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; ?>
    <?php wp_reset_query(); ?>
                </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>
  4. abovegrnd
    Member
    Posted 3 years ago #

    D'oh! I put the code in my test header rather than the live one. That worked like a charm. Thanks, Mark! I knew it was some simple function or "exit" strategy to get me out of the extra loops.

    Cheers!
    Tyler

  5. Mark / t31os
    Moderator
    Posted 3 years ago #

    hehe... happy i could help... ;)

  6. jedwards-kcbt
    Member
    Posted 3 years ago #

    Mark / t31os
    You are the man!
    Mark + google search = happy!!

  7. Cliffo
    Member
    Posted 3 years ago #

    Haha Mark and google for the win once again! My breadcrumb navigation was breaking because of a slider that used get_post and <?php wp_reset_query(); ?> at the end of the slider did the trick. Thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic