【发布时间】:2014-08-30 18:15:27
【问题描述】:
对于我的主页,我想显示特定类别的帖子(例如类别“食物”),而不是默认显示所有帖子。
如何做到这一点?我是否应该使用wp_query() 并避免使用query_posts()?
另外,我需要用wp_reset_postdata(); 重置index.php 中的这个循环吗?
我已经阅读了 codex 并在 Google 上四处搜索,但仍然对更改 Loop 没有信心。
我认为下面的代码可以工作,但我在 WordPress 主题 2012 和 2013 中出现白屏死机。我将 index.php 中的代码替换为
<?php
$query = new WP_Query('cat=1');
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
the_title();
endwhile;
endif;
?>
编辑:
白屏死机是由于我的index.php 文件中的其他地方缺少?>。菜鸟错误。但现在我遇到了分页问题。第 2 页显示与第 1 页相同的帖子。可能有什么问题?
我在functions.php中的分页代码
if ( ! function_exists( 'my_pagination' ) ) :
function my_pagination() {
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'prev_next' => True,
'total' => $wp_query->max_num_pages
) );
}
.
.
编辑:
我在下面使用了 Pieter Goosen 的解决方案。问题已解决。
【问题讨论】:
-
此代码块没有损坏,因此页面上的其他地方可能存在错误。你检查过你的错误日志吗?