简体   繁体   中英

Wordpress pagination

I have a wordpress page that shows the latest news. I want to paginate these news, and this is my code:

 <?php
query_posts('posts_per_page=5');
?>
<?php
while (have_posts()):
    the_post();
?>
   <div class="news"><h1><a href="<?php
    the_permalink();
?>" rel="news" title="<?php
    the_title_attribute();
?>"><?php
    the_title();
?></a></h1>
    <p>
    <?php
    the_excerpt();
?>
   </p>
    </div>
<?php
endwhile;
?>
<p><?php
wp_pagenavi();
?></p>
<?php
wp_reset_query();
?>                                  

I have this code in a page content, i'm using exec-php plugin to parse the code. The problem is that when I click on page 2 I still see the news of the page 1. So pagination isn't working.

Any idea?

On my archive (archive.php) page i've tried the pagination too, but there when I click on page 2 I'm redirected on the homepage instead.

Help me please!

try to use wordpress plugin which will help you for pagination , no need of builtin pagination that was 1 2 3 so on. this plugin gives you better format for pagination.

https://wordpress.org/plugins/jquery-post-splitter/

Usage of this Plugin: https://progtec.wordpress.com/2015/10/13/jquery-post-splitter-plugin/

it might helps you

http://tareq.wedevs.com/2011/07/simple-pagination-system-in-your-wordpress-plugins/

or see below Easy Steps :

$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;

Find total numbers of records

$limit = 10; // number of rows in page
$offset = ( $pagenum - 1 ) * $limit;
$total = $wpdb->get_var( "SELECT COUNT(`id`) FROM {$wpdb->prefix}table_name" );
$num_of_pages = ceil( $total / $limit );

Give limit:

$entries = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}table_name LIMIT $offset, $limit" );

Add this code where you want pagination:

$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '&laquo;', 'aag' ),
    'next_text' => __( '&raquo;', 'aag' ),
    'total' => $total,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}

As far as I know Wordpress reject any URL query parameters that it doesn't recognise.

Even if you solved that problem, this function

<?php query_posts('posts_per_page=5'); ?>

does not accept the page parameter so it seems "obvious" showing always page 1. I don't know the code inside it so I could be wrong.

I've found this link to solve the first problem: http://www.webopius.com/content/137/using-custom-url-parameters-in-wordpress

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM