I have tried multiple solutions but it’s not working.
When I go to /page/2, it doesn’t work.
I’m executing a custom query in index.php of my theme.
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args = array(
'post_type' => array('post', 'music', 'videos'),
'post_status' => 'publish',
//'meta_key' => 'featured',
//'meta_value' => '1',
'posts_per_page' => 10,
'orderby'=>'date',
'order'=>'DESC',
'paged' => $paged
);
query_posts($args);
Here is the link to my website: Home Page of my Site
This page is not working (throwing 404) – Page which is not working(of the format – mywebsite/page/2/)
Just realized this page 2 works – Page which is working (of the format – mywebsite.com/?page=2)
I was having the same problem and this fixed everything for me. This allows me to paginate on index.php as well as my page.php with pretty permalinks.
HTML/PHP:
This allows several things. One it checks if your on home, page, or single and tells the $paged variable how to react in turn. It also allows you to query your pagination with custom post types. Also by not using query_post you get to avoid some really funky stuff that you sometimes get when using it. The paginate(); is a custom function which we bring in now:
Inside your functions.php
This originally came from http://bavotasan.com/2011/simple-pagination-for-wordpress/ with me slightly modding it to get the pagination to work on the homepage.
This, again, does several things. It paginates your page with each page getting it’s own link (which I find nice) and it also re-writes the URL to allow for pretty permalinks. If you check the link, the variable ‘s’ was being used in place of ‘paged’ in part of this. I replaced the ‘s’ with ‘paged’ and everything worked perfectly on my end.
Optional Styling of Pagination
Edit
I realized afterwards that home page pagination would break after clicking one of the page tabs. I’ve fixed this by replacing the conditional statement and putting this in its place. I’ve updated my code above as well.
I sometimes have trouble getting the pagination to work correctly as well. Try using the query below and see if that helps. I’ve just added basically one line of code and changed your query to not use the
$args
variable.I have been struggling with this problem for a while now, it turned our WordPress was not setting one of the required fields for me,
max_num_pages
.So I set this variable within the loop. Here is my sample code.
Upon further reading, I found out this;
(int) $page
Is the page of the post, as specified by the query var page:get_query_var( 'page' )
;(int) $paged
Is the global variable contains the page number of a listing of posts (as in archives).This means that if you are on a page template (which is a single view template) and are using a loop, you will need to set this variable.
A few things here, never change the main query for a custom query on your home page or on any archive page. You’ll always have trouble with pagination.
Also, never use
query_posts
to create a custom query. My emphasis, like in never.If you need to change the behavior the homepage or any archive page, use pre_get_posts` to change/modify the main query before it is executed. This is the correct way to change the main query. By altering the main query before execution, you’ll have no problem with pagination.
pre_get_posts
can be used in conjunction with conditional tags to target specific pages. Have a look at the links provided for examples.