简体   繁体   English

在Wordpress上显示帖子以进行自定义帖子类型查询

[英]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. 我在显示10多个特定帖子类型的帖子时遇到一些问题。 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. 这是我的front-page.php文件的完整代码。 On my first page, there will be 10 posts. 在我的第一页上,将有10个帖子。 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. 在随后的每个页面上,将有15个。这是因为我的帖子显示在五行中,每行显示三个帖子,除了第一页上第一个帖子的宽度是两个“帖子”而三个是“高”时。 There are 50 posts in total for this post type. 此帖子类型总共有50个帖子。

Because of this, there should be only four pages: 因此,应该只有四个页面:

  1. Posts 1 - 10 帖子1-10
  2. Posts 11 - 25 帖子11-25
  3. Posts 26 - 40 帖子26-40
  4. Posts 41 - 50 帖子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. 但是,下一个/上一个链接显示在第4页上,这意味着存在指向第5页的链接,该链接没有任何内容。 In addition, the offset doesn't seem to work, meaning page 4 displays five posts and not ten. 此外,偏移量似乎不起作用,这意味着第4页显示五个帖子,而不是十个。 (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. (通过将!is_paged()更改为get_query_var('paged') != 0 )我添加了一个公式来计算偏移量,以查看是否可以解决该偏移量,即使它确实提供了正确的设置我在其他地方回声时的位置。 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. 我已经尝试了法典中WP_Query页上列出的解决方法(更正了found_posts问题),但是似乎没有什么可以删除第五页。 Even if I manually set the number of posts to 15 for every page, there is still a fifth page. 即使我将每页的帖子数手动设置为15,也仍然有第五页。 I've tried WP_Query and get_posts() , and neither work. 我已经尝试过WP_Queryget_posts() ,但都没有用。 Is there something I have missed? 有什么我想念的吗?

Thanks to Pieter's comment, I have been able to find a fix. 感谢Pieter的评论,我得以找到解决方法。 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: 在我的next_posts_link函数的<?php ?>标记中,在调用函数之前,我添加了以下两行代码:

// 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. 它可能看起来不漂亮,但是可以用,所以我没有抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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