Web制作忘備録

【WP】固定ページでページネーション

トップ MEMORANDAM WORDPRESS

【WP】固定ページでページネーション

カテゴリーぺージにページネーションを入れるのは簡単ですが、固定ページに入れるのはちょっと面倒。
入れることはあまりないけども、案件によっては入れる必要があるかもしれません。
そんな時に役立つよ。

page.php関係のページに書く

page.phpもしくはpage-スラッグ.phpに書き込みます。
page.phpに書くことはほとんどないと思いますので、ページスラッグを打ち間違えないでください。

page-スラッグ.php

    
        <div class="page_news">
            <div class="news_lines">
                    <?php
                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    $the_query = new WP_Query(array(
                        'post_status'    => 'publish',
                        'post_type'      => 'post',
                        'paged'          => $paged,
                        'posts_per_page' => 3,
                        'orderby'        => 'date',
                        'order'          => 'DESC'
                    ));
                    if ($the_query->have_posts()) :
                        while ($the_query->have_posts()) : $the_query->the_post();
                            ?>
                            <a href="<?php the_permalink(); ?>" class="btn_ro">
                                <div class="news_timeline">
                                    <p class="time"><?php the_time('Y.m.d'); ?></p>
                                    <?php
                                    $category = get_the_category();
                                    $cat_name = $category[0]->cat_name;
                                    ?>
                                    <p class="cate"><?php echo $cat_name; ?></p>
                                </div>
                                <div class="news_ttl"><?php the_title(); ?></div>
                            </a>

                        <?php
                        endwhile;
                    else:
                        echo '<div><p>ありません。</p></div>';
                    endif;
                    ?>
            </div>

        </div>

<div class="pagination">
    <div class="pgw">
    <div class="pnavi">
        <?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' => $the_query->max_num_pages,
            'end_size' => 1,
            'mid_size' => 1,
            'current' => ($paged ? $paged : 1),
            'prev_text' => '←',
            'next_text' => '→',
        ));
        ?>
    </div>
    </div>
</div>

まとめ

pagedを入れたり、pagenationのphpも結構めんどいです。

  • 固定ページに入れないようになるように、クライアントを対案や説得を頑張りしましょう!