1

I am using the following code to display media files on a custom static page. Files display properly but pagination is not working. How can I solve this issue?

Following code shows the media files:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'post_type'      => 'attachment',
    'posts_per_page' => '10',
    'paged'          => $paged,
    /* 'numberposts' => -1, */
    'post_status'    => null,
    'author'         => $current_user->ID,
    'post_parent'    => $post->ID,
    /* 'caller_get_posts' => 1, */
);
// The Query
$query = new WP_Query( $args );

$attachments = get_posts( $args );
if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
        echo '<tr><td><a href="' . wp_get_attachment_url( $attachment->ID ) .
            '" rel="shadowbox" title="' . $attachment->post_excerpt . '">';
        echo ($attachment->_wp_attached_file);
        echo '</a>
        </td>';
        </tr>';
    }
}

Added following to functions.php:

if ( ! function_exists( 'my_pagination' ) ) :
    function my_pagination() {
        global $wp_query;       
        $big = 999999999; // need an unlikely integer        
        echo 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'    => True,
            'prev_text'    => __( '« Previous' ),
            'next_text'    => __( 'Next »' ),
            'type'         => 'plain',
            'add_args'     => False,
            'add_fragment' => '',
            'before_page_number' => '',
            'after_page_number'  => ''
        ) );
    }
endif;

Page number or pagination not working, calling the function in my static page like so:

echo my_pagination( $args );

4 Answers 4

1

Dear I have wasted my week on this problem, basically when you select static page as your home page in setting->reading of wordpress the complete behavior of listing things are changed, basically static pages are never meant for pagination the fact is when you call the $paged variable it will always return zero even if you define its value to 1 the pagination will just refresh your page, because its a page not a .php valid file page is already a taxonomy function that is called in index.php of main wordpress the function will always return same thing no matter what you do,

The work around make a index.php file copy all the content from that static page to it and then replace it with your index.php and select latest post in setting->reading of wordpress

Now if you really don't want to mess with it, but still want to have the pagination then you need to write a small code of yours for pagination

basically home.com/page/2 will never work but home.com/?page=2 will always work but changing the permalink structure will change it for every thing so we will create a pagination function that will call next page with home.com/?page=2 structure url

/* ------------------------------------------------------------------*/
/* PAGINATION */
/* ------------------------------------------------------------------*/

//paste this where the pagination must appear

global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!
if ( $total > 1 )  {
     // get the current page
     if ( !$current_page = get_query_var('paged') )
          $current_page = 1;
     // structure of "format" depends on whether we're using pretty permalinks
     if( get_option('permalink_structure') ) {
         $format = '?paged=%#%';
     }
     echo paginate_links(array(
          'base'     => get_pagenum_link(1) . '%_%',
          'format'   => $format,
          'current'  => $current_page,
          'total'    => $total,
          'mid_size' => 4,
          'type'     => 'list'
     ));
}

if you include this code where this rather than the current pagination you will see it working :)

0

Pagination is always based on main query that is in your case is static page and has only one page hence no pagination.

You need to do a simple hack as provided in this answer

0

// Its working on home page set the "paged" parameter (use 'page' if the query is on a static front page)

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1';
$args = array (
    'nopaging'               => false,
    'paged'                  => $paged,
    'posts_per_page'         => '1',
    'post_type'              => 'post',
    'category__not_in' => array(97),
);

// The Query
$query = new WP_Query( $args );
print_r($query);
$total_pages = $query->max_num_pages;


if ($total_pages > 1){

    $current_page = max(1, get_query_var('paged'));
    echo '<div class="navigation">';

    echo paginate_links(array(
        'base' => get_pagenum_link(1) . '%_%',
        'format' => '?paged=%#%',
        'current' => $current_page,
        'total' => $total_pages,
        'type'      => 'list',
        'prev_text'          => __('Previous Posts'),
        'next_text'          => __('More Posts'),
    ));
    echo '</div>';
}
0

The question is quite older but still my answer will be helpful, I hope, as I was in this condition for last 2/3 days and solved it now. The simple fact is

Static front pages uses get_query_var( 'page' ) and not get_query_var( 'paged' ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.