Custom post types in wordpress enables developers to do lot of things. But, when it comes to paginating it - lot of people struggle. I searched the web for the solution but could not find proper solution. So, thought of finding it on my own.
So, I created a custom post type called 'projects'.
Copy the following code in your functions.php
Now, create a template called 'Projects' and assign it to a page.
and copy the following code into it.
In the above code, you should modify the $permalink variable to the current permalink type. I tested only with 'Default' and 'Post name' permalinks.
Also, modify the $post_type variable to the custom post type name.
So, I created a custom post type called 'projects'.
Copy the following code in your functions.php
add_action('init', 'projects_init'); function projects_init() { $project_labels = array( 'name' => _x('Projects', 'post type general name'), 'singular_name' => _x('Projects', 'post type singular name'), 'all_items' => __('All projects'), 'add_new' => _x('Add new project', 'project'), 'add_new_item' => __('Add new project'), 'edit_item' => __('Edit project'), 'new_item' => __('New project'), 'view_item' => __('View project'), 'search_items' => __('Search in projects'), 'not_found' => __('No projects found'), 'not_found_in_trash' => __('No projects found in trash'), 'parent_item_colon' => '' ); $args = array( 'labels' => $project_labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => 5, 'supports' => array('title', 'editor','thumbnail'), 'has_archive' => 'projects' ); register_post_type('projects',$args); }
Now, create a template called 'Projects' and assign it to a page.
and copy the following code into it.
<?php /* Template Name: Projects */ get_header(); $temp = $wp_query; $wp_query = null; $wp_query = new WP_Query(); $show_posts = 4; //How many post you want on per page $permalink = 'Post name'; // Default, Post name $post_type = 'projects'; //Know the current URI $req_uri = $_SERVER['REQUEST_URI']; //Permalink set to default if($permalink == 'Default') { $req_uri = explode('paged=', $req_uri); if($_GET['paged']) { $uri = $req_uri[0] . 'paged='; } else { $uri = $req_uri[0] . '&paged='; } //Permalink is set to Post name } elseif ($permalink == 'Post name') { if (strpos($req_uri,'page/') !== false) { $req_uri = explode('page/',$req_uri); $req_uri = $req_uri[0] ; } $uri = $req_uri . 'page/'; } //Query $wp_query->query('showposts='.$show_posts.'&post_type='. $post_type .'&paged='.$paged); //count posts in the custom post type $count_posts = wp_count_posts('projects'); while ($wp_query->have_posts()) : $wp_query->the_post(); ?> <!--Do stuff--> <h1> <?php the_title(); ?> </h1> <?php the_content(); ?> <?php endwhile;?> <nav> <?php previous_posts_link('« ') ?> <?php $count_post = $count_posts->publish / $show_posts; if( $count_posts->publish % $show_posts == 1 ) { $count_post++; $count_post = intval($count_post); }; for($i = 1; $i <= $count_post ; $i++) { ?> <a <?php if($req_uri[1] == $i) { echo 'class=active_page'; } ?> href="<?php echo $uri . $i; ?>"><?php echo $i; ?></a> <?php } ?> <?php next_posts_link(' »') ?> </nav> <?php $wp_query = null; $wp_query = $temp; // Reset get_footer(); ?>
In the above code, you should modify the $permalink variable to the current permalink type. I tested only with 'Default' and 'Post name' permalinks.
Also, modify the $post_type variable to the custom post type name.
Superb coding. Thanks and you made my day. WordPress Post Type Template
ReplyDeleteThank you Lisa for your valuable comment. Good to know that it helped you.
DeleteThanks a lot for this tutorial
ReplyDeleteGlad to know you liked it..!
DeleteAwesome, great work - much appreciated Tirumal!
ReplyDeleteAs a side note, this created some strange behaviour (404 etc.) for me when I named the page "projects" (i.e. example.com/projects).
Worked perfectly when I changed to /our-projects.
Thanks again!
Great, it works! Thanks for the tutorial, could you please show us how to add dots to the pagination, when there are too many pages?
ReplyDeleteThe information written in the article is descriptive and well written.It is also simple to read and understand.Good Read.
ReplyDeleteWordpress Developers
thanks you so much ....
ReplyDeletebdtender
Actually is the best I have found, looking for an script for wp pagination. Thank you!
ReplyDeleteBut... I have two questions about:
- How I do the style (css) ? where?, I was trying for hours, in different ways and I couldn't obtain.
- What will happen with a large number of pages? by e.g 100 pages or more?
Thank you again!
Regards.
This is beautiful. I'm having one problem: after setting a CPT and making the page template, I cannot see any of the posts I make appearing together on the page. I only see them individually when I click on View within the dashboard. Any idea what I might have forgotten? I have copied your code exactly, changing only the variables you indicated.
ReplyDeleteOh, never mind... I see what I did. Sorry, and nice job again on some beautiful code!
ReplyDeleteHi, i have a problem, I use your code and works fine, but when I try to use it for custom post type taxonomy it won't work :( can you help please
ReplyDeleteHi there, this article has been extremely helpful. I'm using a custom post type and have everything working, apart from the part that adds a class to the active page in the pagination. Does something need to be modified in that part of the code to make this work correctly? I don't think $req_uri[1] on line 59 is right, but I can't figure it out. Can you help?
ReplyDeletesuperb, you are ROCK
ReplyDeleteThank you : )
ReplyDeleteI'm not a Pro but it works for me.
ReplyDeleteReplace the code from line 58
for($i = 1; $i <= $count_post ; $i++) {
if ($paged == 0) {
$paged = 1;
}
?>
And in the link line 59 replace the IF by
if($paged == $i) { echo 'class=active_page'; }
thank you, i started doing a CPT reading this article about custom post type
ReplyDeletehello, thanks for the tutorial it works.
ReplyDeletebtw, any way to make this work with multiple post type?
like $post_type = 'other', 'othe2', 'other3';
Not working with WPML-counting posts in all languages altough not displaying them. Any solution?
ReplyDeleteThank you : )
ReplyDeleteHelp ! I have a custom post type Coupon. Whenever i go to page 2 it shows me a 404 error. Please help, I cant find solutions to it.
ReplyDeleteThis is a truly good site post. Not too many people would actually, the way you just did. I am really impressed that there is so much information about this subject that have been uncovered and you’ve done your best, with so much class. If wanted to know more about green smoke reviews, than by all means come in and check our stuff.
ReplyDeletewordpress tutorial
Thank You and I have a keen supply: Where Do You Get The Money To Renovate A House home renovation services near me
ReplyDelete