-1
對不起,如果我聽起來有點天真.. 我是一個體面的PHP開發人員,最近一直試圖學習wordpress。 任何人都可以告訴我或指向一個教程,告訴我用分頁顯示自定義模板上的帖子的最佳方式?wp
對不起,如果我聽起來有點天真.. 我是一個體面的PHP開發人員,最近一直試圖學習wordpress。 任何人都可以告訴我或指向一個教程,告訴我用分頁顯示自定義模板上的帖子的最佳方式?wp
因爲偶爾(或者大部分的時間,我不知道),你可以得到永久的衝突,因此意外404頁,如果你只是做一個定製query_posts()
然後您添加分頁,鏈接到page-slug/page/2
,page-slug/page/3
,page-slug/page/n
,我通常將分頁設置爲$_GET
參數。
下面是這樣的例子代碼:
<?php
/*
Template Name: Custom Loop Template
*/
get_header();
// Set up the paged variable
$paged = (isset($_GET['pg']) && intval($_GET['pg']) > 0)? intval($_GET['pg']) : 1;
query_posts(array('post_type' => 'post', 'paged' => $paged, 'posts_per_page' => 1));
?>
<?php if (have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<div id="post-<?php echo $post->ID; ?>" <?php post_class(); ?>>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="post-excerpt">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php if ($wp_query->max_num_pages > 1) : ?>
<div class="pagination">
<?php for ($i = 1; $i <= $wp_query->max_num_pages; $i ++) {
$link = $i == 1 ? remove_query_arg('pg') : add_query_arg('pg', $i);
echo '<a href="' . $link . '"' . ($i == $paged ? ' class="active"' : '') . '>' . $i . '</a>';
} ?>
</div>
<?php endif ?>
<?php else : ?>
<div class="404 not-found">
<h3>Not Found</h3>
<div class="post-excerpt">
<p>Sorry, but there are no more posts here... Please try going back to the <a href="<?php echo remove_query_arg('pg'); ?>">main page</a></p>
</div>
</div>
<?php endif;
// Make sure the default query stays intact
wp_reset_query();
get_footer();
?>
這將創建一個自定義的頁面模板,稱爲Custom Loop Template
並會顯示最新的帖子,每頁1。它的底部將有一個基本分頁,從1開始到該查詢的最大頁數。
當然,這只是一個非常基本的例子,但它應該足以讓您找出其餘的問題。