我已为自定义帖子类型设置了存档页reference
, 在这里,我使用查询来获取类型为reference
. 这是可行的,但当我尝试对结果分页时,它不起作用。现在,它甚至没有打印出分页链接。
编辑:这是以前在常规页面上使用的,但我将其更改为存档页面。那就是分页中断的时候。
这里是源代码;
自定义帖子类型reference
(插件)
<?php
/*
comments here removed
*/
?>
<?php
/* Custom post types */
function create_references_post_type() {
$labels = array(
\'name\' => __( \'References\', \'kasparabi\' ),
\'singular_name\' => __( \'Reference\', \'kasparabi\' ),
\'add_new\' => __( \'Add New\', \'kasparabi\' ),
\'add_new_item\' => __( \'Add new reference\', \'kasparabi\' ),
\'edit_item\' => __( \'Edit reference\', \'kasparabi\' ),
\'new_item\' => __( \'New reference\', \'kasparabi\' ),
\'all_items\' => __( \'All references\', \'kasparabi\' ),
\'view_item\' => __( \'View reference\', \'kasparabi\' ),
\'search_items\' => __( \'Search references\', \'kasparabi\' ),
\'not_found\' => __( \'No references found\', \'kasparabi\' ),
\'not_found_in_trash\' => __( \'No references found in the Trash\', \'kasparabi\' ),
\'parent_item_colon\' => \'\',
\'menu_name\' => __(\'References\', \'kasparabi\')
);
$args = array(
\'labels\' => $labels,
\'description\' => __(\'All the references\', \'kasparabi\'),
\'public\' => true,
\'menu_position\' => 5,
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\'),
\'has_archive\' => __(\'references\', \'kasparabi\')/*,
\'rewrite\' => array(\'slug\' => __(\'references\', \'kasparabi\'))*/
);
register_post_type( \'reference\', $args );
}
add_action(\'init\', \'create_references_post_type\');
function add_post_type_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( \'post_type\', array( \'reference\' ) );
return $query;
}
add_action( \'pre_get_posts\', \'add_post_type_to_query\' );
?>
我查询帖子的方式:
$wp_query = new WP_Query(array(
\'post_type\' => \'reference\',
\'posts_per_page\' => 9,
\'paged\' => (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1
));
如何打印分页链接:
<div class="col-xs-12">
<?php previous_posts_link(__(\'← newer\', \'kasparabi\')); ?>
<?php if ($loop->max_num_pages > $paged) :
next_posts_link(__(\'older →\', \'kasparabi\'), $posts_per_page);
endif; ?>
</div>
作为一个额外的问题;除了每次都创建一个新的查询之外,还有其他方法获得帖子吗?