Singlepost.php上同一类别的上一篇和下一篇帖子

时间:2017-06-12 作者:varsha

我想在wordpress中显示以前和以后的同类帖子

我的代码是:

        <?php
       $prev_post = get_previous_post();
       $next_post = get_next_post();
       ?>

       <nav class="post-nav section">
        <div class="row no-margin">
        <?php // Display the thumbnail of the previous post ?>
        <div class="col-xs-12 col-sm-6 prev-post" style="padding:0px">                                                 
           <?php
         $prevPost = get_previous_post();
            $prevthumbnail = get_the_post_thumbnail($prevPost->ID); 
            $post_tile=get_the_title($prevPost->ID);
            $thumb = wp_get_attachment_image_src(    
              get_post_thumbnail_id($prevPost->ID), \'full\' );                                                
            ?>

            <div class="post-nav-thumb" style="background-image:  
             url(\'<?php echo $thumb[\'0\'];?>\')" ></div>                                  
            <div class="post-nav-content">
            <div class="post-nav-subtitle"><?php  
            previous_post_link(\'%link\', \'Prev Post\'); ?></div>                                          
            <h2 class="post-nav-title"><span><?php     
            previous_post_link(\'%link\', $post_tile); ?></span></h2>
            </div>

        </div>

    <?php // Display the thumbnail of the next post ?>
        <div class="col-xs-12 col-sm-6 next-post" style="padding:0px"> 
          <?php      
            $nextPost = get_next_post();
            $nextthumbnail = get_the_post_thumbnail($nextPost->ID); 
            $post_tile=get_the_title($nextPost->ID);
            $thumb = wp_get_attachment_image_src(  
            get_post_thumbnail_id($nextPost->ID), \'full\' );
            ?>
            <div class="post-nav-thumb" style="background-image: 
            url(\'<?php echo $thumb[\'0\'];?>\')"></div>
            <div class="post-nav-content">
            <div class="post-nav-subtitle"><?php next_post_link(\'%link\', 
            \'Next Post\'); ?></div>
            <h2 class="post-nav-title"><span><?php 
           next_post_link(\'%link\', $post_tile); ?></span></h2>
            </div>

        </div>
    </div></nav>

1 个回复
SO网友:Syed Afaque

我不会使用get\\u posts()在单曲上设置循环。php页面。我只会使用正常的正确循环。请查看codex中关于主题开发的页面

这里是一个单一的例子。将按预期工作的php

<div id="primary" class="content-area">
    <div id="content" class="site-content" role="main">
        <?php
            // Start the Loop.
            while ( have_posts() ) : the_post(); ?>

        <?php
                get_template_part( \'content\', get_post_format() );

                // Previous/next post navigation.
                previous_post_link( \'%link\', \'Prev post in category\', true );
                next_post_link( \'%link\', \'Next post in category\', true );

                // If comments are open or we have at least one comment, load up the comment template.
                if ( comments_open() || get_comments_number() ) {
                    comments_template();
                }
            endwhile;
        ?>
    </div><!-- #content -->
</div><!-- #primary -->

正如另一个答案所指出的,去阅读一下如何使用next_post_link 以及previous_post_link

结束

相关推荐

Bulk update wordpress posts

编码器。我对WP编码真的很陌生,我没有任何知识,我们到了。我创建了一个插件(实际上找到了它,但我做了一些修改),更新了我所有的wp帖子。让我们向您展示代码,if ( ! class_exists( \'MyPlugin_BulkUpdatePosts\' ) ) : class MyPlugin_BulkUpdatePosts { public function __construct() { register_activa