BLOGブログ
WordPress archive.phpのページャー実装
アーカイブページに大量の投稿がある場合、全てを1ページに表示するとページが長くなりすぎてしまいます。ページャーを使うことでユーザーは必要な情報にスムーズにアクセスでき、SEOの観点でも効果的です。
<?php get_header(); ?>
<div class=”archive-posts”>
<?php if (have_posts()) : ?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li><div class=”date”><?php the_time(‘Y.m.d’); ?></div><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; ?>
</div>
<div class=”archive-pager”>
<?php global $wp_rewrite;
$paginate_base = get_pagenum_link(1);
if(strpos($paginate_base, ‘?’) || ! $wp_rewrite->using_permalinks()){
$paginate_format = ”;
$paginate_base = add_query_arg(‘paged’,’%#%’);
}
else{
$paginate_format = (substr($paginate_base,-1,1) == ‘/’ ? ” : ‘/’) .
user_trailingslashit(‘page/%#%/’,’paged’);;
$paginate_base .= ‘%_%’;
}
echo paginate_links(array(
‘base’ => $paginate_base,
‘format’ => $paginate_format,
‘total’ => $wp_query->max_num_pages,
‘mid_size’ => 4,
‘current’ => ($paged ? $paged : 1),
‘prev_text’ => ‘<‘,
‘next_text’ => ‘>’,
)); ?>
</div>
<?php get_footer(); ?>
CSSでスタイルを調整してください。
CONTACTお問い合わせ
サービスに関するお問い合わせやお見積もり・ご相談などお気軽にご相談ください。