Reputation: 45
This use to work, until wordpress updated and now the pagination on a custom page is repeating the first page. Page 2 for example will just show up page 1 content.
I've been looking and looking and nothing is helping.
I'm calling wordpress functions outside of wordpress install with
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
then the wordpress loop is (and it's probably outdated and faulty, but this was the only one that worked at the time)
$paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 20,
//'showposts' => 20,
'orderby'=> 'menu_order',
'paged' => $paged,
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock',
'compare' => '=',
)
)
);
query_posts( $args); ?>
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/producttemplate.php";
include_once($path);
?>
</div>
<br class="clear" />
<div class="wrapper">
<div class="pagination">
<p><br /><?php pagination_bar($args); if(!$_GET['viewall']){ ?>
<br />
<a class="all" href="<?php echo add_query_arg( array( 'view' => 'all' ), get_pagenum_link(1) ); ?>">Show All</a><br /></p>
<?php } }?>
<?php wp_reset_query(); ?>
and then my custom pagination function is
//Pagenation
function pagination_bar() {
global $wp_query;
$total_pages = $wp_query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
echo paginate_links(array(
'base' => @add_query_arg('paged','%#%'),
'format' => '?paged=%#%',
'current' => $paged,
'total' => $total_pages,
));
}
}
Thank you
Upvotes: 0
Views: 1717
Reputation: 45
After pulling whats left of my hair out, I solved it. It was this part
require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
Needs to be
require $_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php';
I'm currently using both as a fallback. I don't know if it needs to just be one. If someone has more info if it's safe to use both?
Upvotes: 0
Reputation: 96
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'charters',
'cat'=> $cat,
'post_per_page'=> 6,
'paged' => $paged
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p><?php
endwhile;
}
wp_reset_query();
?>
<?php echo pnavigation( $query ); ?>
then you need to add following code to function.php :
function pnavigation( $wp_query ) {
$big = 999999999; // need an unlikely integer
$pages = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => false,
'type' => 'array',
'prev_next' => TRUE,
'prev_text' => '←',
'next_text' => '→',
) );
if( is_array( $pages ) ) {
$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
echo '<ul class="pagination pagination-lg">';
foreach ( $pages as $page ) {
echo "<li>$page</li>";
}
echo '</ul>';
}
}
also add this code to function.php, you need to just replace url (client-testimonials) with your page url
function pagination_rewrite() {
add_rewrite_rule('client-testimonials/page/?([0-9]{1,})/?$', 'index.php?pagename=client-testimonials&paged=$matches[1]', 'top');
}
add_action('init', 'pagination_rewrite');
Upvotes: 0
Reputation: 2057
The following are two lines copied out of your second code dump:
$current_page = max(1, get_query_var('paged'));
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
Notice the query_var is paged
?
Now, have a look at this copy from the first line of the first dump:
$paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
Notice the difference? page
, instead of paged
.
Upvotes: 1