简体   繁体   中英

Wordpress - Pagination

i'm creating a blog page in my custom theme wordpress, i would like use pagination in my list posts, my query with list of posts work well but the pagination doesn't work. i see always the same first 2 posts.

page-blog.php

/*
 * Template Name: Pagina Blog
 */

<?php get_header(); ?>

<?php


$posts_per_page = 2;

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

$args = array(
  'posts_per_page' => $posts_per_page,
    'paged' => $paged,
  'offset'           => 0,
  'category'         => '',
  'category_name'    => '',
  'orderby'          => 'date',
  'order'            => 'DESC',
  'include'          => '',
  'exclude'          => '',
  'meta_key'         => '',
  'meta_value'       => '',
  'post_type'        => 'post',
  'post_mime_type'   => '',
  'post_parent'      => '',
  'author'     => '',
  'author_name'    => '',
  'post_status'      => 'publish',
  'suppress_filters' => true 
);
//$posts_array = get_posts( $args );
$wp_query = new WP_Query( $args );
//query_posts( $args ); 

?>


  <?php while ( have_posts() ) : the_post(); ?>

  <!-- Title -->

    <h1 class="my-4"><?php the_title() ?></h1>

    <h2 class="card-title">
      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </h2>

  <!-- Content -->

    <?php the_content() ?>

  <!-- Pagination -->

    <?php next_posts_link(); ?>
    <?php previous_posts_link(); ?>

  <?php endwhile;

  // Reset Query
  wp_reset_query(); ?>

<?php get_footer(); ?>

I think there some issue with the loop, but i'm not sure, i can see "Newer" and "Older" links but when i go on older i see the same posts, so "www.mysite.com/blog" have the same post of "www.mysite.com/blog/2" :(

Here lies your problem:

'offset' => 0,

Replace that with:

'offset' => $posts_per_page * ($paged - 1)

Alternativley, if I'm reading the documentation correctly, you can maybe just remove the offset value entirely.

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