[英]How to query posts for a custom post type in a custom taxonomy ? in wordpress 3.0
[英]Display posts on Wordpress for custom post type query
我在显示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('« Older Entries') ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
</section>
<?php get_footer(); ?>
这是我的front-page.php
文件的完整代码。 在我的第一页上,将有10个帖子。 在随后的每个页面上,将有15个。这是因为我的帖子显示在五行中,每行显示三个帖子,除了第一页上第一个帖子的宽度是两个“帖子”而三个是“高”时。 此帖子类型总共有50个帖子。
因此,应该只有四个页面:
但是,下一个/上一个链接显示在第4页上,这意味着存在指向第5页的链接,该链接没有任何内容。 此外,偏移量似乎不起作用,这意味着第4页显示五个帖子,而不是十个。 (通过将!is_paged()
更改为get_query_var('paged') != 0
)我添加了一个公式来计算偏移量,以查看是否可以解决该偏移量,即使它确实提供了正确的设置我在其他地方回声时的位置。 我已经尝试了法典中WP_Query页上列出的解决方法(更正了found_posts
问题),但是似乎没有什么可以删除第五页。 即使我将每页的帖子数手动设置为15,也仍然有第五页。 我已经尝试过WP_Query
和get_posts()
,但都没有用。 有什么我想念的吗?
感谢Pieter的评论,我得以找到解决方法。 根据法典页面,我可以设置要显示的最大页面数。 手动设置该数字只是一个简单的问题。
在我的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;
添加了注释以使您受益; 它们不包含在我的文件中。 虽然我可能应该添加它们。 通过添加过多的帖子以完全适合页面进行测试表明它可以正常工作。 它可能看起来不漂亮,但是可以用,所以我没有抱怨。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.