php - WordPress paginate_links - 如何使用它?

标签 php wordpress twitter-bootstrap wordpress-4.5.2

我想向页面及其子页面添加数字分页。这是我想要创建的分页(从 Bootstrap ):

<nav>
  <ul class="pagination">
    <li>
      <a href="#" aria-label="Previous">
        <span aria-hidden="true">&laquo;</span>
      </a>
    </li>
    <li><a href="#">1</a></li>
    <li><a href="#">2</a></li>
    <li><a href="#">3</a></li>
    <li><a href="#">4</a></li>
    <li><a href="#">5</a></li>
    <li>
      <a href="#" aria-label="Next">
        <span aria-hidden="true">&raquo;</span>
      </a>
    </li>
  </ul>
</nav>

这是我的代码:

<!-- block -->
<div class="row grid events-block">

    <?php
    $parent = $post->ID;
    // query_posts('posts_per_page=15&post_type=page&post_parent='.$parent);
    query_posts(array('posts_per_page'=>'1', 'post_type' => 'page', 'post_parent' => $parent, 'paged' => get_query_var('paged')));
        while (have_posts()) : the_post();
    ?>

    <!-- item -->
    <div class="grid-item col-md-6 col-sm-6 col-xs-12 event-item">

        <div class="date-box">
            <?php echo $event_dates; ?>
        </div>

        <div class="event-item-text-box">
            <div class="event-item-text-inner-box">
                <h3 class="heading-item"><a href="#" target="_blank"><?php the_title(); ?></a></h3>
                <p><?php the_excerpt(); ?></p>
            </div>
        </div>

    </div>
    <!-- item -->
     <?php endwhile; ?>

     <?php
     // Reset the post to the original after loop. otherwise the current page becomes the last item from the while loop.
     // https://codex.wordpress.org/Function_Reference/wp_reset_query
     wp_reset_query();
     ?>

</div>
<!-- block -->

<?php $args = array(
    'base'               => '%_%',
    'format'             => '?paged=%#%',
    'total'              => 1,
    'current'            => 0,
    'show_all'           => false,
    'end_size'           => 1,
    'mid_size'           => 2,
    'prev_next'          => true,
    'prev_text'          => __('« Previous'),
    'next_text'          => __('Next »'),
    'type'               => 'plain',
    'add_args'           => false,
    'add_fragment'       => '',
    'before_page_number' => '',
    'after_page_number'  => ''
); ?>

<?php echo paginate_links( $args ); ?>

但我无法让它工作。我错过了什么想法吗?

最佳答案

Pagination Like : Prev 1 2 3 Next

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$data= new WP_Query(array(
    'post_type'=>'YOUR_POST_TYPE', // your post type name
    'posts_per_page' => 3, // post per page
    'paged' => $paged,
));

if($data->have_posts()) :
    while($data->have_posts())  : $data->the_post();
            // Your code
    endwhile;

    $total_pages = $data->max_num_pages;

    if ($total_pages > 1){

        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    }
    ?>    
<?php else :?>
<h3><?php _e('404 Error&#58; Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

您可以尝试一下上面的代码吗?我认为这对你有帮助。

关于php - WordPress paginate_links - 如何使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38939212/

相关文章:

php - WordPress自定义主题白色背景问题

php - 在 Symfony2 中使用同一包中的两个实体管理器

php - $_SERVER ['REQUEST_URI' ] 和 $_GET

WordPress Foreach 只显示一个结果

javascript - Bootstrap : divs overlapping the jumbotron

jquery - 如何使左侧网格列内容在滚动过程中保持不变?

css - 具有固定大小和滚动条的 Bootstrap 缩略图

php - PDO php 中的一些查询正在执行,有些则没有

php - 从具有动态表头的文件导入 csv

wordpress - 有没有办法检查wordpress日志?比如管理员执行了哪些操作等?