下一篇文章和上一篇文章的缩略图(精选图片)没有正确显示!

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

我用这段代码显示上一篇和下一篇文章的缩略图(特色图片)(一种称为Blocks的自定义文章类型)。

(自定义循环)

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

    <?php // Display previous and next posts thumbnails     
    ?>  <div class="float-left"> <?php
            $prevPost = get_previous_post();
            $prevthumbnail = get_the_post_thumbnail($prevPost->ID);
            previous_post_link(\'%link\', $prevthumbnail);
    ?>  </div>

    <?php // Get thumbnail of next post
    ?>  <div class="float-right"> <?php
            $nextPost = get_next_post();
            $nextthumbnail = get_the_post_thumbnail($nextPost->ID);
            next_post_link(\'%link\', $nextthumbnail);
    ?>  </div>
    </div>
以下是年表:

enter image description here

这是输出:

  <div class="block-2 border-top">
            <h2><a href="http://localhost/wpac/?blocks=mainbar" title="Permalink to Mainbar" rel="bookmark">Mainbar</a></h2>

          <div class="float-left"> <a href="http://localhost/wpac/?blocks=mainbar-left" rel="prev"><img width="160" height="150" src="http://localhost/wpac/wp-content/uploads/2011/02/showcase2.png" class="attachment-post-thumbnail wp-post-image" alt="showcase2" title="showcase2" /></a>  </div>

          <div class="float-right"> <a href="http://localhost/wpac/?blocks=mainbar-right" rel="next"><img width="160" height="150" src="http://localhost/wpac/wp-content/uploads/2011/02/2974999772_7085da4d347.jpg" class="attachment-post-thumbnail wp-post-image" alt="2974999772_7085da4d347" title="2974999772_7085da4d347" /></a> </div>
        </div>

      <div class="block-3 border-top">
            <h2><a href="http://localhost/wpac/?blocks=sidebar" title="Permalink to Sidebar" rel="bookmark">Sidebar</a></h2>
            <a href="http://localhost/wpac/?blocks=sidebar" title="Permalink to Sidebar" rel="bookmark"><img width="160" height="150" src="http://localhost/wpac/wp-content/uploads/2011/02/2974999772_7085da4d3410.jpg" class="attachment-post-thumbnail wp-post-image" alt="2974999772_7085da4d3410" title="2974999772_7085da4d3410" /></a>
            <p></p>

            <p></p>
        </div>
我在哪个岗位上并不重要。的缩略图Mainbar LeftMainbar Right 始终显示)

有什么建议吗?

EDIT:

完整代码:

<?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 // Create and run custom loop ?>
            <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(); ?>

3 个回复
最合适的回答,由SO网友:Peter Rowell 整理而成

Update: 好啊如果你继续修改问题,我可以删除之前写的内容,重新开始。

老实说,我对你想做什么有点困惑。由于这篇文章似乎只针对一篇文章(基于模板顶部的评论),我不清楚前两个自定义循环的目的是什么。看起来像是在打印所有的缩略图和内容blocks 位置为的帖子Work, 不管你在哪一个帖子上。如果您只是想显示当前单个帖子的缩略图和内容,那么只需这样做。

注意:因为Front Page 发布电话the_post(), 它正在修改全局$post. 我不知道你的边栏代码是否取决于那是不是原始帖子,但我打电话给wp_reset_query() 以防万一。

以下是我认为您试图实现的目标,但我可能错了:

<?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 if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

    <div class="block-1">
            <?php the_post_thumbnail(\'large\'); ?>
        </div>

        <div class="block-2 padding-top">
            <?php the_content(); ?>
        </div>

        <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 endif; ?>

        <?php // Create and run custom loop
            $custom_posts = new WP_Query();
            $custom_posts->query(\'post_type=blocks&amp;location=Front Page&amp;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 wp_reset_query(); // I don\'t know if this is needed by the sidebar code. If not, you can delete this. ?>
        <?php get_sidebar(\'secondary\'); ?>
    </div><!-- #content -->

    <?php get_footer(); ?>

SO网友:Pabline

看起来您在不需要的时候设置了2个循环。如果您所在的模板显示“blocks”帖子(例如single blocks.php),那么只需使用get\\u previous\\u post和get\\u next\\u post函数即可。

类似于:

<?php 
    the_post();
?><div class="float-left"> <?php
        $prevPost = get_previous_post();
        $prevthumbnail = get_the_post_thumbnail($prevPost->ID);
        previous_post_link(\'%link\',\'\'.$prevthumbnail.\'\');
?>  </div>

 <div class="float-right"> <?php
        $nextPost = get_next_post();
        $nextthumbnail = get_the_post_thumbnail($nextPost->ID);
        next_post_link(\'%link\',\'\'.$nextthumbnail.\'\');
?>  </div>
刚刚注意到您在循环之外,在这种情况下,请设置查询,使其只返回一篇文章。将post ID添加到WP\\U查询参数中。

SO网友:janoChen

我需要这个:

<?php wp_reset_query(); ?>
此处放置:

<?php endwhile; ?>
<?php wp_reset_query(); ?>

    <?php // Display post thumbnail

结束

相关推荐