我想使用 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; ?>`

试试这个代码,

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

相关内容