繁体   English   中英

如何为Wordpress中的帖子添加数字分页

[英]How to add number pagination for posts in wordpress

我在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>

尝试这个

    <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' => __('&laquo;'),
        'next_text' => __('&raquo;'),
        '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>";
    }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM