[英]How do add the blog next prev navigation?
我對日記帖子使用自定義帖子類型,但是我想為上一頁和下一頁添加導航。 因此,如果我的日記頁面上有5個以上的帖子,我將在下一個鏈接或2、3、4等數字上進行分頁。如果沒有更多的帖子,則它應該顯示在前。 現在,我的日記頁面在index.php上。
在我的wordpress閱讀設置中,我在首頁顯示中使用了靜態頁面。 我的首頁是front-page.php,帖子頁面是期刊頁面。 博客頁面最多顯示5個帖子。 聯合供稿源顯示了最近的5條帖子。
如何使用自定義帖子類型“新聞”添加下一個和上一個導航?
<?php
get_header();
?>
<!-- journal -->
<section class="container-wrap">
<?php
$args = array('post_type' => 'journals');
$query = new WP_Query($args);
while($query -> have_posts()) : $query -> the_post();
?>
<article class="post-wrap">
<header>
<a href="<?php the_permalink(); ?>" class="post-title">
<h1 class="post-title"><?php the_title(); ?></h1>
</a>
<span class="post-date"><?php echo(types_render_field('date', array('format' => 'm.d.Y') )); ?></span>
</header>
</article>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</section>
<!-- /journal -->
<?php
get_footer();
?>
將paginate_links
掛鈎與自定義WP_Query
數組結合使用。 確保為查詢指定paged
數組參數。 這將設置查詢以返回分頁結果。
<?php
// 1- Setup paging parameter
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
// 2- Setup WP_query variable to get last 12 posts
$args = array(
'posts_per_page' => 12,
'post_type' => 'journals',
'orderby' => 'most_recent',
'paged' => $paged,
);
$the_query = new WP_Query( $args );
// 3- Setup new loop
if($the_query->have_posts()) :
while($the_query->have_posts()) :
$the_query->the_post();
// 4- Output parameters you want here
echo '<div class="col-md-4">';
echo '<h4><a href="' . the_permalink() . 'title="Read more">' . the_title() . '</a></h4>';
echo '<a href="' . the_permalink() . '">' . the_post_thumbnail() . '</a>';
echo the_excerpt();
echo '</div>';
// 5- close up loop
endwhile;
endif;
// 6- Output paginate_links just below post loop
echo paginate_links( array(
'base' => str_replace( 999999, '%#%', esc_url( get_pagenum_link( 999999 ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
// 7- reset post data query
wp_reset_postdata();
?>
試試我的代碼,它可以在我的網站上運行http://www.thehiddenwhy.com/blog/,請點擊此鏈接如何在wordpress的頁面代碼中創建分頁?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.