【问题标题】:Display posts on Wordpress for custom post type query在 Wordpress 上显示帖子以进行自定义帖子类型查询
【发布时间】:2016-02-19 12:21:48
【问题描述】:

我在显示超过 10 个特定帖子类型的帖子时遇到了一些问题。我目前的代码是:

<?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(); ?>

这是我的front-page.php 文件的完整代码。在我的第一页上,将有 10 个帖子。在随后的每一页上,将有 15 个。这是因为我的帖子显示在五行中,每行三个帖子,除了在第一页上,第一个帖子是两个“帖子”宽和三个“帖子”高。此帖子类型共有 50 个帖子。

因此,应该只有四页:

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

但是,下一个/上一个链接出现在第 4 页上,这意味着有一个指向没有任何内容的第 5 页的链接。 此外,偏移似乎不起作用,这意味着第 4 页显示了 5 个帖子而不是 10 个。(通过将 !is_paged() 更改为 get_query_var('paged') != 0 来修复)我添加了一个公式来计算偏移以查看是否可以修复它,它没有,即使当我在其他地方回显它时它确实给出了正确的位置。我已经尝试了 Codex 中 WP_Query 页面上列出的解决方法(更正了 found_posts 问题),但似乎没有什么可以删除第五页。即使我手动将每页的帖子数设置为 15,仍然有第五页。我试过WP_Queryget_posts(),但都不管用。有什么我遗漏的吗?

【问题讨论】:

  • 请从 nav.php 发布代码。你在哪里运行这段代码?来自存档-{POST TYPE} 模板文件?
  • 已更新完整代码。代码从我的front-page.php 模板文件运行。
  • 如果这是静态首页,您的分页效果如何。您没有将自定义查询传递给next_posts_link),因此next_posts_link 依赖于主查询。静态首页上的主要查询只能有一页。如果还有更多,则说明您在某处有一个过滤器干扰了主查询对象,或者页面上的某些内容在后台使用了query_posts。此外,pagination 不是有效参数,因此您可以删除它
  • 从我在 codex 上看到的情况来看,next_posts_link 只有两个变量可以使用——链接文本和最大页数——所以我不能传递任何其他内容.我已经更改了链接以使用$query-&gt;max_num_pages 来说明最大页数,然后又添加了六个帖子。第 51 到 55 篇文章将出现在第 4 页,第 56 篇文章将出现在第 5 页。现在,我可以看到到第 4 页的链接,这解决了我的原始查询,但我丢失了第 5 页的链接。不管怎样,它都是一样的无论我使用的是front-page.php 还是archive-{POST TYPE}.php 文件。
  • 其实,这确实给了我一个想法……我可以尝试强制设置最大页数并将其用作第二个变量。

标签: post pagination wordpress


【解决方案1】:

感谢 Pieter 的评论,我找到了解决办法。根据codex页面,我可以设置显示的最大页数。只需手动设置该数字即可。

在我的next_posts_link 函数的&lt;?php ?&gt; 标记中,就在我调用该函数之前,我添加了这两行代码:

// 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; 

为您的利益添加了评论;它们不包含在我的文件中。虽然我可能应该添加它们。通过添加太多帖子以完全适合页面进行测试表明它有效。它可能看起来不太漂亮,但它确实有效,所以我没有抱怨。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    相关资源
    最近更新 更多