Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Pagination not working

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)

like image 308
dang Avatar asked Nov 29 '22 13:11

dang


1 Answers

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:

<?php
    //Fix homepage pagination
    if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }

    $temp = $wp_query;  // re-sets query
    $wp_query = null;   // re-sets query
    $args = array( 'post_type' => array('post', 'music', 'videos'), 'orderby'=>'date', 'order'=>'DESC', 'posts_per_page' => 10, 'paged' => $paged);
    $wp_query = new WP_Query();
    $wp_query->query( $args );
    while ($wp_query->have_posts()) : $wp_query->the_post(); 
?>

<!--your loop stuff here -->

<?php endwhile; ?>
<nav>
   <?php paginate(); ?>
   $wp_query = null;
   $wp_query = $temp; // Reset
</nav>

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

function paginate() {
global $wp_query, $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;

$pagination = array(
    'base' => @add_query_arg('page','%#%'),
    'format' => '',
    'total' => $wp_query->max_num_pages,
    'current' => $current,
    'show_all' => true,
    'type' => 'list',
    'next_text' => '&raquo;',
    'prev_text' => '&laquo;'
    );

if( $wp_rewrite->using_permalinks() )
    $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'page', get_pagenum_link( 1 ) ) ) . '?page=%#%/', 'paged' );

if( !empty($wp_query->query_vars['s']) )
    $pagination['add_args'] = array( 's' => get_query_var( 's' ) );

echo paginate_links( $pagination );
}

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

ul.page-numbers {
    margin: 20px 0 10px;
    width: 100%;
    padding: 0;
    font-size: 12px;
    line-height: normal;
    clear: both;
    float: left;
}

ul.page-numbers li {
       float: left;
    }

ul.page-numbers a,
ul.page-numbers span {
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    background: -webkit-gradient(linear, left top, left bottom, from(#E4E3E3), to(#FFFFFF));
    background: -moz-linear-gradient(top,  #E4E3E3,  #FFFFFF);
    filter:  progid:DXImageTransform.Microsoft.gradient(startColorstr='#E4E3E3', endColorstr='#FFFFFF');
    padding: 3px 4px 2px 4px; 
    margin: 2px;
    text-decoration: none;
    border: 1px solid #ccc;
    color: #666;
}

ul.page-numbers a:hover,
ul.page-numbers span.current {  
    border: 1px solid #666;
    color: #444;
}

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.

$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'page', get_pagenum_link( 1 ) ) ) . '?page=%#%/', 'paged' );
like image 59
GregT Avatar answered Dec 19 '22 01:12

GregT