Post 在Wordpress上显示帖子以进行自定义帖子类型查询
我在显示10多个特定帖子类型的帖子时遇到了一些问题。我目前拥有的代码是:Post 在Wordpress上显示帖子以进行自定义帖子类型查询,post,pagination,wordpress,Post,Pagination,Wordpress,我在显示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'
<?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('« Older Entries') ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
</section>
<?php get_footer(); ?>
多亏了彼特的评论,我才找到了解决办法。根据codex页面,我可以设置要显示的最大页数。只需手动设置该数字即可
在调用函数之前,我在我的next\u posts\u link
函数的
标记中添加了以下两行代码:
// 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;
//获取找到的帖子总数
$pc=$query->found\u posts;
//检查$pc是否小于11。
//如果是这样,从$pc中删除10(对于第一页上的帖子),然后将其除以15(对于其他页面上的帖子)。
//将1添加到第一页的帐户。
//否则,返回1,因为只有一页。
$mnp=($pc<11)?1:ceil($pc-10)/15)+1;
为您的利益添加了评论;它们不包括在我的文件中。虽然我可能应该加上它们。通过在页面上添加过多的帖子来测试它,结果表明它是有效的。它可能看起来不漂亮,但它可以工作,所以我没有抱怨。请发布nav.php中的代码。你在哪里运行这个代码?从存档{POST TYPE}模板文件?用完整代码更新。代码是从myfrontpage.php
模板文件运行的。如果这是一个静态frontpage,那么分页是如何工作的。您没有将自定义查询传递到next\u posts\u link)
,因此next\u posts\u link
依赖于主查询。静态首页上的主查询只能有一个页面。如果还有更多,那么您的某个地方有一个过滤器干扰了主查询对象,或者页面上的某个东西正在引擎盖下使用query\u posts
。另外,pagination
不是一个有效的参数,因此您可以从我在codex上看到的内容中删除它,只有两个变量可用于next\u posts\u link
——链接文本和最大页数——因此我无法向其中传递任何其他内容。我用$query->max_num_pages
更改了链接以说明最大页数,然后又添加了六篇文章。帖子51到55将出现在第4页,帖子56将出现在第5页。现在,我可以看到到第4页的链接,这解决了我原来的查询,但我丢失了到第5页的链接。无论我是使用我的首页.php
还是我的存档-{POST TYPE}.php
文件,它都是完全相同的。实际上,这确实给了我一个想法。。。我可以尝试强制设置最大页数,并将其用作第二个变量。