为什么上一个POST_LINK和NEXT_POST_LINK在循环之外工作?

时间:2011-02-12 作者:janoChen

This is my single.php file:

我在用previous_post_link();next_post_link();.

在Worpress codex中说it only works inside the loop.

<?php
/**
 * 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();
            $custom_posts->query(\'post_type=blocks&location=Work&section=Intro\');
            while ($custom_posts->have_posts()) : $custom_posts->the_post();
        ?>  <div class="block-1">
                <?php the_post_thumbnail(\'large\'); ?>
            </div>
        <?php endwhile; ?>

        <?php // Create and run custom loop
            $custom_posts = new WP_Query();
            $custom_posts->query(\'post_type=blocks&location=Work&section=Tagline\');
            while ($custom_posts->have_posts()) : $custom_posts->the_post();
        ?>  <div class="block-2 padding-top">
                <?php the_content(); ?>
            </div>
        <?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); ?>
                </div>

            <?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); ?>
                </div>
            </div>

        <?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>
            </div>
        <?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(); ?>

2 个回复
最合适的回答,由SO网友:Rarst 整理而成

“内部循环”本质上意味着函数依赖于全局变量(例如$post) 循环运行时设置的。请注意,这不一定是由query_posts().

在您的特定代码中,这些变量由$custom_posts->the_post() 打电话之后wp_reset_query() 将这些值恢复到初始状态。

SO网友:Deepak

尝试此操作以获取下一个和上一个帖子外部循环的链接

获取之前帖子使用的链接previous_post_link();

获取下一篇文章使用的链接next_post_link();

结束

相关推荐