简体   繁体   中英

Display posts on Wordpress for custom post type query

I am having a few issues with displaying more than 10 posts of a particular post type. The code I have currently is:

<?php get_header();
$ppp = ( !is_paged() ) ? '10' : '15';
$ofs = ( get_query_var('paged') != 0 ) ? ( ( get_query_var('paged') - 1 ) * 15 ) - 5 : 0;
// WP_Query arguments
$args = array (
    'post_type'              => array( 'book' ),
    'pagination'             => true,
    'paged'                  => get_query_var('paged'),
    'posts_per_page'         => $ppp,
    'offset'                 => $ofs,
    'ignore_sticky_posts'    => false,
);

// The Query
$query = new WP_Query( $args ); ?>
    <section class="books <?php if(get_query_var('paged') == 0){ echo 'first-page'; } else { echo 'next-page'; } ?>">
      <?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
      <?php $fImg = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' ); $fi = $fImg[0]; ?>
      <a href="<?php the_permalink(); ?>" class="grid-item-story" style="background-image: url('<?php echo $fi; ?>');">
        <article <?php post_class(); ?> id="book-<?php the_ID(); ?>">
          <div class="book-item-wrap">
            <div class="book-item-inner">
              <div class="book-item-content">
                <div class="book-item-content-inner">
                  <p class="published-date"><?php the_time('M j, Y'); ?></p>
                  <?php if ($query->current_post == 0 && !is_paged() ) { ?>
                    <h1><?php the_title(); ?></h1>
                  <?php } else { ?>
                    <h1><?php echo getBookTitle(get_the_title()); ?></h1>
                  <?php } ?>
                  <div><span class="button">Read</span></div>
                </div>
              </div>
            </div>
          </div>
        </article>
      </a>
      <?php endwhile; endif; ?>
      <div class="clear"></div>
      <div class="navigation">
        <div class="next-posts"><?php next_posts_link('&laquo; Older Entries') ?></div>
        <div class="prev-posts"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
      </div>
    </section>
<?php get_footer(); ?>

This is the full code of my front-page.php file. On my first page, there will be 10 posts. On every subsequent page, there will be 15. This is because my posts are displayed in five rows of three posts per row, except on the first page where the first post is two "posts" wide and three "posts" tall. There are 50 posts in total for this post type.

Because of this, there should be only four pages:

  1. Posts 1 - 10
  2. Posts 11 - 25
  3. Posts 26 - 40
  4. Posts 41 - 50

However, the next / previous links are appearing on page 4, meaning there is a link to a page 5 which does not have any content. In addition, the offset doesn't seem to work, meaning page 4 displays five posts and not ten. (Fixed by changing !is_paged() to get_query_var('paged') != 0 ) I've added a formula to calculate the offset to see if that would fix it, which it doesn't, even though it does give the correct position when I echo it elsewhere. I have tried the workaround listed on the WP_Query page in the Codex (correcting for the found_posts issue), but nothing seems to remove that fifth page. Even if I manually set the number of posts to 15 for every page, there is still a fifth page. I've tried WP_Query and get_posts() , and neither work. Is there something I have missed?

Thanks to Pieter's comment, I have been able to find a fix. According to the codex page, I can set the maximum number of pages to display. It just became a simple matter of manually setting that number.

In the <?php ?> tag for my next_posts_link function, just before I called the function, I added these two lines of code:

// Get the total number of posts found
$pc = $query->found_posts;
// Check if $pc is less than 11.
// If so, delete 10 from $pc (for the posts on the first page), then divide it by 15 (for the posts on other pages).
// Add 1 to account for the first page.
// Otherwise, return 1, because there will only be one page.
$mnp = ( $pc < 11 ) ? 1 : ceil( ( $pc - 10 ) / 15 ) + 1; 

Comments have been added for your benefit; they aren't included in my file. Although I probably should add them. Testing it by adding one too many posts to cleanly fit on a page has shown it works. It probably doesn't look pretty, but it works, so I'm not complaining.

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