我想使用PHP在WordPress中添加分页。但我不知道如何在自定义插件中进行操作

时间:2018-07-24 06:28:12

标签: php wordpress

嘿,我将此代码添加到WordPress页面中,此插件获取特定类别的所有帖子,其工作正常,我想在此代码上添加分页,但我不知道该怎么做。

<?php
 $catquery = new WP_Query( 'cat=124&posts_per_page=10' );
 while($catquery->have_posts()) : $catquery->the_post()?><div class="main-box">

<div class="photos">
    <ul class="photo-data">
               <li><?php  the_post_thumbnail();  ?></li>
    </ul>
</div>
<div class="title_desc">


<ul>
     <li style="list-style-type: none;">
          <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
              <?php  $content = get_the_content();
              echo substr($content, 0, 50);  ?>

           <a href="<?php the_permalink() ?>" rel="bookmark">...Read More</a>
     </li>
</ul>


</div></div><?php endwhile; ?>`

1 个答案:

答案 0 :(得分:-1)

尝试此代码,

<?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $catquery = new WP_Query( 'cat=7&posts_per_page=5&paged='.$paged);
        global $wp_query;
        $tmp_query = $wp_query;
        $wp_query = null;
        $wp_query = $catquery;
        while($catquery->have_posts()) : $catquery->the_post()?>
        <div class="main-box">
            <div class="photos">
                <ul class="photo-data">
                    <li>
                        <?php  the_post_thumbnail();  ?>
                    </li>
                </ul>
            </div>
            <div class="title_desc">
                <ul>
                    <li style="list-style-type: none;">
                        <h3><a href="<?php the_permalink() ?>" rel="bookmark">
                            <?php the_title(); ?>
                        </a></h3>
                        <?php  $content = get_the_content();
                        echo substr($content, 0, 50);  ?>
                        <a href="<?php the_permalink() ?>" rel="bookmark">...Read More</a>
                    </li>
                </ul>
            </div>
        </div>
        <?php endwhile; ?>
        <div class="pagination">
          <?php previous_posts_link('&laquo; Newer') ?>
          <?php next_posts_link('Older &raquo;') ?>
        </div>