LoginSignup
0
6

More than 5 years have passed since last update.

[WordPress] 固定ページでタグ(カテゴリー、ターム)一覧を出してかつページ送りも実装

Last updated at Posted at 2018-01-15

やりたいこと

  • タグ(カテゴリー、ターム)一覧を表示したい
  • 数が多くなったらページ送りしたい。

WordPressは自動でタグ(カテゴリー、ターム)一覧のページを生成しないのでよしなにやる必要があります。

コード

単純にWordPress上にあるタグ全ての情報を得るだけなら get_terms() でいいけど、ページ送りしようと思うとちょっと面倒よね、という話で。
固定ページに埋め込む想定。page-{slug}.php など作って他に影響出ないようにしましょう。

<?php
    $terms = get_terms( 'post_tag', array(
        'orderby' => 'count', 
    ));

    if ( ! empty( $terms ) ) {
        $html           = '';
        $posts_per_page = 5; // 1ページあたりのタームの表示数
        $count = 1;
        foreach ( $terms as $term ) {
            $html .= '<div><a href="' . get_term_link( $term ) . '">' . esc_html( $term->name ) . '</a></div>';
            if ( $count === $posts_per_page ) {
                $html .= '<!--nextpage-->';
                $count = 1;
            } else {
                $count++;
            }
        }
        $html = rtrim( $html, '<!--nextpage-->' );
        $post->post_content = rtrim( $html, '<!--nextpage-->' );

        setup_postdata( $post );
    }
    the_content();
    wp_link_pages( array(
        'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'iemoto' ),
        'after'  => '</div>',
    ) );
    wp_reset_postdata();
?>

現場からは以上です。

0
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
6