我的wordpress网站上有30个帖子。并在页面中显示4个帖子。我怎样才能为它们添加编号分页?
这是我的代码:
<div class="eventssec hidden-xs hidden-sm">
<?php
$type = 'posts';
$args=array(
'post_type' => $type,
'posts_per_page'=>4,
'caller_get_posts'=>1,
'order'=>'asc'
);
$my_query = new WP_Query($args);
$i=0;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="events">
<figure>
<?php
$image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</figure>
<div class="desctext">
<h2><?php the_field('name'); ?></h2>
<p><?php the_field('designition'); ?></p>
<p><?php the_field('emp-id'); ?></p>
</div>
</div>
<?php $i++; endwhile; wp_reset_query(); ?>
</div>
答案 0 :(得分:2)
试试这个
<div class="eventssec hidden-xs hidden-sm">
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$type = 'posts';
$args=array(
'post_type' => $type,
'posts_per_page'=>4,
'caller_get_posts'=>1,
'paged' => $paged,
'order'=>'asc'
);
$my_query = new WP_Query($args);
$i=0;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="events">
<figure>
<?php
$image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</figure>
<div class="desctext">
<h2><?php the_field('name'); ?></h2>
<p><?php the_field('designition'); ?></p>
<p><?php the_field('emp-id'); ?></p>
</div>
</div>
<?php $i++; endwhile;
my_custom_pagination($my_query->max_num_pages, "", $paged);
wp_reset_query(); ?>
</div>
<?php
function my_custom_pagination($numpages = '', $pagerange = '', $paged = '', $search = false) {
if (empty($pagerange)) {
$pagerange = 2;
}
/**
* This first part of our function is a fallback
* for custom pagination inside a regular loop that
* uses the global $paged and global $wp_query variables.
*
* It's good because we can now override default pagination
* in our theme, and use this function in default queries
* and custom queries.
*/
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if (!$numpages) {
$numpages = 1;
}
}
if ($search) {
$format = '&paged=%#%';
} else {
$format = 'page/%#%';
}
/**
* We construct the pagination arguments to enter into our paginate_links
* function.
*/
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => $format,
// 'format' => '&paged=%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
//echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
}