Wordpress自定义帖子类型循环的分页

时间:2015-12-04 09:41:44

标签: wordpress pagination custom-paging

我无法为我的自定义帖子类型循环进行分页工作,我正在使用骨骼主题。

页面导航功能

function bones_page_navi() {
  global $wp_query;
  $bignum = 999999999;
  if ( $wp_query->max_num_pages <= 1 )
    return;
  echo '<nav class="pagination col-md-12">';
  echo paginate_links( array(
    'base'         => str_replace( $bignum, '%#%', esc_url( get_pagenum_link($bignum) ) ),
    'format'       => '',
    'current'      => max( 1, get_query_var('paged') ),
    'total'        => $wp_query->max_num_pages,
    'prev_text'    => '&larr;',
    'next_text'    => '&rarr;',
    'type'         => 'list',
    'end_size'     => 3,
    'mid_size'     => 3
  ) );
  echo '</nav>';

循环

$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

$query_args = array(
    'post_type' => 'Operators',
    'orderby'   => 'title',
    'order'     =>  'ASC',
    'posts_per_page' => 2,
    'paged' => $paged
);

$the_query = new WP_Query( $query_args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

注册自定义帖子类型

function custom_post_types() {

    register_post_type( 'operator',
        array( 'labels' => array(
            'name' => __( 'Operator', 'bonestheme' ), /* This is the Title of the Group */
            'singular_name' => __( 'Operator', 'bonestheme' ), /* This is the individual type */
            'all_items' => __( 'All Operators', 'bonestheme' ), /* the all items menu item */
            'add_new' => __( 'Add New', 'bonestheme' ), /* The add new menu item */
            'add_new_item' => __( 'Add New Operator', 'bonestheme' ), /* Add New Display Title */
            'edit' => __( 'Edit', 'bonestheme' ), /* Edit Dialog */
            'edit_item' => __( 'Edit Operators', 'bonestheme' ), /* Edit Display Title */
            'new_item' => __( 'New Operator', 'bonestheme' ), /* New Display Title */
            'view_item' => __( 'View Operator', 'bonestheme' ), /* View Display Title */
            'search_items' => __( 'Search Operators', 'bonestheme' ), /* Search Custom Type Title */
            'not_found' =>  __( 'Nothing found in the Database.', 'bonestheme' ), /* This displays if there are no entries yet */
            'not_found_in_trash' => __( 'Nothing found in Trash', 'bonestheme' ), /* This displays if there is nothing in the trash */
            'parent_item_colon' => ''
            ), /* end of arrays */
            'description' => __( 'This is the example custom post type', 'bonestheme' ), /* Custom Type Description */
            'public' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'show_ui' => true,
            'query_var' => true,
            'menu_position' => 8, /* this is what order you want it to appear in on the left hand side menu */
            'menu_icon' => get_stylesheet_directory_uri() . '/library/images/custom-post-icon.png', /* the icon for the custom post type menu */
            'rewrite'   => array( 'slug' => 'operator', 'with_front' => false ), /* you can specify its url slug */
            'has_archive' => 'operator', /* you can rename the slug here */
            'capability_type' => 'post',
            'hierarchical' => true,
            /* the next one is important, it tells what's enabled in the post editor */
            'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'sticky')
        ) /* end of options */
    ); /* end of register post type */
}

在隔离链接甚至不显示的那一刻,我试图找到实际问题所在的位置。我确实让它们在某一点上显示但在/ page / 2上有404错误

1 个答案:

答案 0 :(得分:-1)

<?php 
function vb_pagination( $query=null ) {
  global $wp_query;
  $query = $query ? $query : $wp_query;
  $big = 999999999;
  $paginate = paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'type' => 'array',
    'total' => $query->max_num_pages,
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'prev_text' => __('Prev'),
    'next_text' => __('Next'),
    )
  );
  if ($query->max_num_pages > 1) :
?>
<div>
<ul class="pagination">
  <?php
  foreach ( $paginate as $page ) {
    echo '<li>' . $page . '</li>';
  }
  ?>
</ul>
</div>
<?php  endif; } ?>

显示分页:

<?php if ( function_exists('vb_pagination') ) {
vb_pagination( $the_query );
} ?>

尝试使用$ paged变量

if ( get_query_var('paged') ) {
           $paged = get_query_var('paged');
        } else if ( get_query_var('page') ) {
           $paged = get_query_var('page');
        } else {
           $paged = 1;
        }