WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is my single.php file:

I'm using previous_post_link(); and next_post_link();.

In the Worpress codex it says that it only works inside the loop.

 * The Template for displaying all single posts.
 * @package WordPress
 * @subpackage Starkers
 * @since Starkers 3.0

    get_header(); ?>

    <?php get_sidebar(); ?>

    <div id="content">
        <?php // Create and run custom loop
            $custom_posts = new WP_Query();
            while ($custom_posts->have_posts()) : $custom_posts->the_post();
        ?>  <div class="block-1">
                <?php the_post_thumbnail('large'); ?>
        <?php endwhile; ?>

        <?php // Create and run custom loop
            $custom_posts = new WP_Query();
            while ($custom_posts->have_posts()) : $custom_posts->the_post();
        ?>  <div class="block-2 padding-top">
                <?php the_content(); ?>
        <?php endwhile; ?>
        <?php wp_reset_query(); ?>

        <?php // Display the thumbnails of previous and next posts ?>
            <div class="block-2 border-top">
            <?php // Display the thumbnail of the previous post ?>
                <div class="float-left"> <?php
                    $prevPost = get_previous_post();
                    $prevthumbnail = get_the_post_thumbnail($prevPost->ID); ?>
                    <h2><?php previous_post_link('%link', 'Previous'); ?></h2>
                    <?php previous_post_link('%link', $prevthumbnail); ?>

            <?php // Display the thumbnail of the next post ?>
                <div class="float-right"> <?php
                    $nextPost = get_next_post();
                    $nextthumbnail = get_the_post_thumbnail($nextPost->ID); ?>
                    <h2><?php next_post_link('%link', 'Next'); ?></h2>
                    <?php next_post_link('%link', $nextthumbnail); ?>

        <?php // Create and run custom loop
            $custom_posts = new WP_Query();
            $custom_posts->query('post_type=blocks&location=Front Page&section=Sidebar');
            while ($custom_posts->have_posts()) : $custom_posts->the_post();
        ?>  <div class="block-3 border-top">
                <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_post_thumbnail(); ?></a>
                <p><?php the_excerpt(); ?></p>
                <p><?php echo get_post_meta($post->ID, "Other_Work", true); ?></p>
        <?php endwhile; ?>

        <?php // Start the main loop
            if ( have_posts() ) while ( have_posts() ) : the_post();
        ?>  <div class="block-4 border-top">
                <?php the_content(); ?>
            </div><!-- .entry-content -->
        <?php endwhile; // end of the loop. ?>

        <?php get_sidebar('secondary'); ?>
    </div><!-- #content -->

    <?php get_footer(); ?>
share|improve this question
up vote 3 down vote accepted

"Inside the Loop" essentially means that function relies on data from global variables (such as $post) that are set up when Loop runs. Note that this is not necessarily done by main Loop of query_posts().

In your specific code those variables are filled by $custom_posts->the_post() calls and after that wp_reset_query() gets those values back to initial state.

share|improve this answer
@Rast I'ma bit confused, I thought everything outside of any while...endwhile wouldn't count as being in any loop. (in this case, outside of the while...endwhile of the custom loops) – janoChen Feb 12 '11 at 21:27
@janoChen if I am not mistaken it's not really possible (or at least practical) to try and detect if you are inside of specific while loop. The only thing that matters for WP is content of its global variables and that can be manipulated in multiple ways (with or without while or any other kind of control structure). – Rarst Feb 12 '11 at 21:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.