0
votes

I would like to add pagination above my posts on wordpress blog page, where should I place the code? I'm using:

<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array( ..parameters here.. ) );
?> 

and a custom loop. If I place it below the loop it works fine, if I place it above the loop it doesn't show up. Is there solution for this?

My lopp looks something like:

<?
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;   
$args = array(
    'paged' => $paged,
    'posts_per_page'=>12,
    'orderby' => 'meta_value_num',
    'order' => 'DSC',
    'pagination' => 'true',
    'cat' => '2'
);

$the_query = new WP_Query( $args );
     if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) { 
?>  
<?php $the_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>"> ...</li> ....   
2
Another thing beside my answer is visit remicorson.com/… It may help you.Prafulla Kumar Sahu

2 Answers

0
votes

I would suggest you to use custom hook before and after the loop , if possible, that will help you to do something like below code , that can be used in woocommerce, but if you are not using woocommerce, you need to define your CUSTOM HOOKS before and after the loop , to do something like this . You have tried to override the template, if you will follow my suggestion then also you need to add hooks in your template, so I would suggest if possible try to do it within the template by overriding it or better to do it only in functions.php without modifying the template.but for now, I can suggest you this.

remove_action( 'woocommerce_after_shop_loop', 'woocommerce_pagination', 10 );
add_action( 'woocommerce_before_shop_loop', 'woocommerce_pagination', 10 );
function woocommerce_pagination(){
  echo '<nav class="woocommerce-pagination">'; ?>
  <?php
    echo paginate_links( apply_filters( 'woocommerce_pagination_args', array(
      'base'        => str_replace( 999999999, '%#%', get_pagenum_link( 999999999 ) ),
      'format'      => '',
      'current'     => max( 1, get_query_var('paged') ),
      'total'       => $wp_query->max_num_pages,
      'prev_text'   => '&larr;',
      'next_text'   => '&rarr;',
      'type'        => 'list',
      'end_size'        => 3,
      'mid_size'        => 3
    ) ) );
0
votes

try this :

I added paginate_links function before while loop.and added $the_query to paginate_links->'total' parameter

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;   
$args = array(
    'paged' => $paged,
    'posts_per_page'=>2,
    'post_type'=>'post'        
);

    $the_query = new WP_Query( $args );
     if ( $the_query->have_posts() ) 
     {       
        $big = 999999999; 
        echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $the_query->max_num_pages
        ) );         

        while ( $the_query->have_posts() ) 
        { 
            $the_query->the_post(); 
            echo $post->ID;
        }
    }