Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

やりたいこと

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

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();
?>

現場からは以上です。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away