【发布时间】:2019-03-16 19:47:11
【问题描述】:
我有一个带有自定义 WP 查询的页面,用于输出与奖励帖子类型相关的不同帖子。
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 2,
'ignore_sticky_posts' => 1,
'post_type' => 'award',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
's' => $_GET['keyword'],
'paged' => $paged;
);
我正在使用默认的wordpress分页功能,例如
paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => $paged,
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'mid_size' => 3,
'prev_next' => false,
'add_args' => false,
'add_fragment' => '',
));
如果我提交这个 url awards/?keyword=test,我会得到相关的结果,页码很少。如果我单击分页上的第 2 页,该页面将重定向到此 url awards/page/2/?keyword=test 并且我得到一个找不到页面的错误!如果我访问awards/page/2/,我会得到同样的错误!我做错了什么?
如果我能像awards/?page=2&keyword=test这样在url中保留我的查询字符串也很好。
更新
如果删除奖励自定义帖子类型但我将帖子保留在数据库中,则使用过滤器的分页将正常工作。
此外,WP 查询位于一个名为“奖励”的页面中,其中包含大量奖励。
我的奖励自定义帖子类型:
$cpt = array(
'singular' => 'Article',
'plural' => 'Awards & Press',
'type' => 'award',
'slug' => 'awards'
);
register_post_type( $cpt['type'],
array(
'labels' => array(
'name' => sprintf( '%s', $cpt['plural'] ),
'singular_name' => sprintf( '%s', $cpt['singular'] ),
'add_new' => 'Add New',
'add_new_item' => sprintf( 'Add New %s', $cpt['singular'] ),
'edit' => 'Edit',
'edit_item' => sprintf( 'Edit %s', $cpt['singular'] ),
'new_item' => sprintf( 'New %s', $cpt['singular'] ),
'all_items' => sprintf( 'All %s', $cpt['plural'] ),
'view' => sprintf( 'View %s', $cpt['singular'] ),
'search_items' => sprintf( 'Search %s', $cpt['plural'] ),
'not_found' => sprintf( 'No %s Found', $cpt['plural'] ),
'not_found_in_trash' => sprintf( 'No %s Found In Trash', $cpt['plural'] ),
'parent_item_colon' => ''
),
'description' => 'Create a new item',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'publicly_queryable' => true,
'has_archive' => false,
'rewrite' => array(
'slug' => $cpt['slug'],
'with_front' => false,
),
'menu_position' => 5,
'show_in_nav_menus' => true,
'menu_icon' => 'dashicons-admin-post',
'hierarchical' => false,
'query_var' => true,
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail',
)
)
);
另一个更新
如果我改变:
'rewrite' => array(
'slug' => $cpt['slug'],
'with_front' => false,
),
到
'rewrite' => false,
过滤器的分页工作,但是我失去了漂亮的网址awards/post-name/,它变成了awards/?award=post-name。
我需要使用过滤器进行分页,同时保持帖子的漂亮网址。
【问题讨论】:
标签: php wordpress pagination query-string