drl2051 2016-10-13 00:41
浏览 18

如何在特定类别中使用Wordpress下一个/上一个postlink

I am new in wordpress. I have limit my post from my products category to 6, but in the reading settings of my wordpress it is by default post per page = 10. I want to do the next/previous postlink only from my products category where i have limiting it to 6 post per page:

<?php
        $args = array(
            'post_type' => 'post',
            'category_name' => 'products',
            'posts_per_page' => '6',
            );
        $productsBlog = new WP_Query( $args );
?>

I want to put next/previous post link within only in products category without adjusting the reading settings in wordpress and adjust the postperpage by default that is equal to 10 post per page.

Here is my fullcode with my next/previous postlink:

<?php
        $args = array(
            'post_type' => 'post',
            'category_name' => 'products',
            'posts_per_page' => '6',
        );
        $productsBlog = new WP_Query( $args );

        $count = 0;
        if( $productsBlog->have_posts() ) : 
                ?>
        <div class="container">
            <ul class="image">
            <div class="row row-centered">
                <?php while ( $productsBlog->have_posts() ) : $productsBlog->the_post(); ?>
                    <div class="col-xs-4 col-box1" onMouseOver="show_title<?php echo $count ?>()" onMouseOut="hide_title<?php echo $count ?>()">
                    <li class="top-featured-image">
                    <span class="effect" id="hoverli<?php echo $count ?>"><?php the_title( sprintf('<a href="%s">', esc_url(get_permalink())),'</a>'); ?></span>
                    <?php the_post_thumbnail('productsize'); ?>
                    <?php the_title(); ?>
                    </div>
                    <?php if($count==2) :
                    echo '</div>';
                    echo '<div class="row row-centered">';
                    endif; ?>
                </li>
            <?php $count++; endwhile; ?>
            </ul>
            </div>
        </div>
        <div class="row controller-post">
            <div class="col-md-6">
                <?php next_posts_link('<< Older Posts'); ?>
            </div>
            <div class="col-md-6">
                <?php previous_posts_link('Newer Posts >>'); ?>
            </div>
        </div>
        <br><br>

        <?php endif; 
        wp_reset_postdata();
        ?>
  • 写回答

1条回答 默认 最新

  • doujinai2183 2016-11-03 23:00
    关注

    Check out the documentation:

    Next, Prev WP-link documentation

    You should be able to set the third parameter to true and it will show next and previouse post relevant to that posts current category. If you need to apply a custom taxonomy, you can add the last parameter as shown below:

    <?php next_posts_link('', '<< Older Posts', true, '', 'products'); ?>
    

    and

    <?php previous_posts_link('', 'Newer Posts >>', true, '', 'products'); ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口