I have set up an archive page for my custom post type reference
, where I use a query to get all posts of type reference
. This works, but when I try to paginate the result, it doesn’t work. Right now, it doesn’t even print out the pagination links.
EDIT: This was earlier used on a regular page, but I changed it to an archive page. That’s when the pagination broke.
Here is the source code;
Custom post type reference
(plugin)
<?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' );
?>
The way I query for the posts:
$wp_query = new WP_Query(array(
'post_type' => 'reference',
'posts_per_page' => 9,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
How I print out the pagination links:
<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>
And as a bonus question; is there another way to get the posts than creating a new query every time?
Shouldn’t it be like this?
And next_posts_link/previous_posts_links has these arguments:
$label is not a string in your code but a kind of an array?
I am not so familiar with these “magical PHP functions”, but what do you suppose these “__” to do? As far as I am informed, one should not use these.
You register your post type with the name of
reference
in this case you can use the built in template hierarchy to get the correct loop. Create a file in the root of your theme folder with the namearchive-reference.php
.In this file you don’t have to query the references because it will be automatic by WordPress. The only thing that you have to write in this file is a simple logic:
This will list your references with their title and excerpt.
Don’t forget to allow rewrite in your post type declaration:
If you want to query your references on a page, you always have to build a custom query. This answer could help you how to do it: https://stackoverflow.com/a/20905538/2043492
Hope this helps!