對於我的主頁,我想顯示特定類別(例如類別「食物」)的帖子,而不是所有帖子的默認顯示。爲主頁的特定類別循環
這是如何實現的?是真的,我應該使用wp_query()
並避免query_posts()
? 此外,我是否需要在index.php
中使用wp_reset_postdata();
重置此循環?
我已經閱讀了代碼和google搜索,但仍然沒有信心改變循環。
我想下面的代碼的工作,但我得到的死亡的白色屏幕在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
文件中其他地方丟失了?>
。 Noob錯誤。但是現在我遇到了分頁問題。第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的解決方案。問題修復。
該代碼塊沒有被破壞,所以可能在頁面上的其他地方出現了錯誤。你有沒有檢查你的錯誤日誌? – rmmoul 2014-08-30 18:45:05