显示自定义帖子类型时,我的分页遇到一些问题。我想显示9篇文章,然后显示数字分页。这是可行的,一些链接是用(对我来说)正确的URL生成的:http://mywebsite/tutorial/page/2 或http://mywebsite/tutorial/taxonomy/page/2 但它总是在404页上完成。
欢迎您提出任何想法,以下是我的代码,如果您发现任何问题:)
提前谢谢。
西里尔
<?php
$args = array(
\'post_type\' => \'tutorial\',
\'paged\' => ((get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1),
\'posts_per_page\' => 9
);
query_posts($args);
?>
[MAIN LOOP]
<?php numeric_pagination(); ?>
以下是numeric\\u pagination()函数(在web上找到,在其他网站上工作…):
function numeric_pagination($pages = \'\', $range = 2) {
global $paged;
$showitems = ($range * 2) + 1;
if(empty($paged)) $paged = 1;
if($pages == \'\') {
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages) {
$pages = 1;
}
}
if(1 != $pages) {
echo \'<div class="numeric-pagination"><p>\';
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo \'<a href="\'.get_pagenum_link(1).\'">«</a>\';
if($paged > 1 && $showitems < $pages) echo \'<a href="\'.get_pagenum_link($paged - 1).\'">‹</a>\';
for ($i=1; $i <= $pages; $i++) {
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
echo ($paged == $i)? \'<span class="current">\'.$i.\'</span>\':\'<a href="\'.get_pagenum_link($i).\'" class="inactive" >\'.$i.\'</a>\';
}
}
if ($paged < $pages && $showitems < $pages) echo \'<a href="\'.get_pagenum_link($paged + 1).\'">›</a>\';
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo \'<a href="\'.get_pagenum_link($pages).\'">»</a>\';
echo \'</p></div>\';
}
}
我的自定义帖子类型和分类法可能有问题,因此我也给出了相应的代码:
/* POST TYPE */
add_action(\'init\',\'create_tutorials_post_type\');
function create_tutorials_post_type() {
// Labels
$labels = array(
\'name\' => \'Tutorials\',
\'singular_name\' => \'Tutorial\',
\'add_new\' => \'Add new\',
\'add_new_item\' => \'Add new tutorial\',
\'edit_item\' => \'Edit\',
\'new_item\' => \'New tutorial\',
\'view_item\' => \'View tutorial\',
\'search_items\' => \'Search tutorial\',
\'not_found\' => \'No tutorial found\',
\'not_found_in_trash\' => \'No tutorial found in trash\',
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Tutorials\'
);
// Arguments
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => true,
\'capability_type\' => \'post\',
\'has_archive\' => true,
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array(\'title\',\'editor\',\'author\',\'thumbnail\',\'excerpt\',\'comments\')
);
// Register post type
register_post_type(\'tutorial\',$args);
}
/* TAXONOMY */
add_action(\'init\',\'create_tutorials_taxonomies\');
function create_tutorials_taxonomies() {
// Labels
$labels = array(
\'name\' => \'Tutorial types\',
\'singular_name\' => \'Tutorial type\',
\'search_items\' => \'Search a type\',
\'all_items\' => \'All types\',
\'parent_item\' => \'Parent type\',
\'parent_item_colon\' => \'Parent type:\',
\'edit_item\' => \'Edit type\',
\'update_item\' => \'Update type\',
\'add_new_item\' => \'Add new type\',
\'new_item_name\' => \'New tutorial type\',
\'menu_name\' => \'Types\'
);
// Arguments
$args = array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array(\'slug\' => \'tutorials\', \'hierarchical\' => true)
);
// Register taxonomy
register_taxonomy(\'tutorial_type\',array(\'tutorial\'),$args);
}