我正试图在网站首页显示每个帖子的第一个图片附件。下面的代码适用于其他页面,但分页不适用于is_front_page()
.
显示1-20个缩略图的第一页,单击“下一步”将进入显示21-40个缩略图的第2页,以此类推。
我已经重置了永久链接,PHP日志中没有错误。
有什么想法吗?
function homepage_grid() {
global $wpdb, $post;
?>
<div id="content">
<div class="post_box">
<div class="grid_thumbs">
<?php
$limit = 20;
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
query_posts(\'showposts=\' . $limit . \'&paged=\' . $paged . \'&orderby=date&order=DESC\');
?>
<?php while (have_posts()) : the_post();
$attachment = array_values( get_children( array( \'post_parent\' => $post->ID, \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => \'ASC\', \'numberposts\' => 1 ) ) );
var_dump($attachment);
?><div class="thumbnails"><a href="<?php the_permalink() ?>" title="<?php the_title(\'\', \'\'); ?>"><?php if( $attachment ) echo \'<img src="\' . wp_get_attachment_thumb_url($attachment[0]->ID) . \'" />\'; ?></a></div>
<?php endwhile; ?>
<div class="clear"></div>
<?php post_navigation(); ?>
<br />
</div> <!-- div.grid_thumbs close -->
<div class="clear"></div>
</div>
</div>
<?php }
function post_navigation() {
global $wp_query;
if ($wp_query->is_home || $wp_query->is_archive || $wp_query->is_search) {
if ($wp_query->max_num_pages > 1) {
$previous = \'Previous Entries\';
$next = \'Next Entries\';
echo "\\t\\t\\t<div class=\\"prev_next\\">\\n";
if ($wp_query->query_vars[\'paged\'] <= 1) {
echo "\\t\\t\\t\\t<p class=\\"previous\\">";
next_posts_link($previous);
echo "</p>\\n";
}
elseif ($wp_query->query_vars[\'paged\'] < $wp_query->max_num_pages) {
echo "\\t\\t\\t\\t<p class=\\"previous floated\\">";
next_posts_link($previous);
echo "</p>\\n";
echo "\\t\\t\\t\\t<p class=\\"next\\">";
previous_posts_link($next);
echo "</p>\\n";
}
elseif ($wp_query->query_vars[\'paged\'] >= $wp_query->max_num_pages) {
echo "\\t\\t\\t\\t<p class=\\"next\\">";
previous_posts_link($next);
echo "</p>\\n";
}
echo "\\t\\t\\t</div>\\n\\n";
}
}
}
最合适的回答,由SO网友:Charles Clarkson 整理而成
根据WP_Query docs:
分页说明:使用get\\u query\\u var(\'page\');如果希望查询在已设置为静态首页的页面模板中工作。查询变量“page”还保存单个分页帖子或帖子内容中包含Quicktag的页面的页码。
在静态首页上显示当前页面的帖子:
$paged = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
$query = new WP_Query( array( \'paged\' => $paged ) );