Support » Plugins » Pagination wordpress static page not working

  • I am using following code to display media files on a customize static page. File display properly but pagination is not working. Can anyone please guide me to solve this issue.

    Following code shows the media files

    <?php 
    
    	$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

    /* Function */
    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

    <?php echo my_pagination( $args ); ?>

Viewing 1 replies (of 1 total)
  • 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 homecom/?page=2 will always work but changing the permalink structure will change it for every one so we will create a pagination function that will call next page with home.com/?page=2

    /* ——————————————————————*/
    /* 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 rather than the current pagination you will see it working 🙂

Viewing 1 replies (of 1 total)
  • The topic ‘Pagination wordpress static page not working’ is closed to new replies.