サポート » 使い方全般 » ログイン中のユーザーのカテゴリ一覧リンクを出したい

  • 解決済 kokorog

    (@kokorog)


    ログイン中のユーザーの
    ・カテゴリ一覧(記事数つき)
    ・タクソノミー一覧(記事数つき)
    を表示させたいと思っています。

    ページを作り、そのページの独自テンプレートの中に

    ・カテゴリ一覧

    <ul>
    <?php
        $args = array(
    	'author'             => $current_user->ID,
    	'show_option_all'    => '',
    	'orderby'            => 'count',
    	'order'              => 'desc',
    	'style'              => 'list',
    	'show_count'         => 1,
    	'hide_empty'         => 1,
    	'use_desc_for_title' => 1,
    	'child_of'           => 0,
    	'feed'               => '',
    	'feed_type'          => '',
    	'feed_image'         => '',
    	'exclude'            => '',
    	'exclude_tree'       => '',
    	'include'            => '',
    	'hierarchical'       => 1,
    	'title_li'           => '',
    	'show_option_none'   => __( '' ),
    	'number'             => null,
    	'echo'               => 1,
    	'depth'              => 2,
    	'current_category'   => 0,
    	'pad_counts'         => 1,
    	'taxonomy'           => 'category',
        );
        wp_list_categories( $args );
    ?>
    </ul>

    ・タクソノミー一覧

    <?php
    $taxonomy = 'items';
    $args = array(
        'author'     => $current_user->ID,
        'parent'     => 0,
        'pad_counts' => true,
        'hide_empty' => false
    );
    $terms = get_terms( $taxonomy , $args );
    if ( count( $terms ) != 0 ) {
    	echo '<ul>';
        foreach ( $terms as $term ) {
            $term = sanitize_term( $term, $taxonomy );
            $term_link = get_term_link( $term, $taxonomy );
            if ( is_wp_error( $term_link ) ) {
                continue;
            }
            echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>&nbsp;(' . $term->count . ')';
            $term_children = get_term_children( $term->term_id, $taxonomy );
            if( count( $term_children ) != 0 ) {
                echo '<ul>';
                foreach ( $term_children as $term_id ) {
                    $term_child = get_term_by( 'id', $term_id, $taxonomy );
                    $term_child = sanitize_term( $term_child, $taxonomy );
                    $term_child_link = get_term_link( $term_child, $taxonomy );
                    if ( is_wp_error( $term_child_link ) ) {
                        continue;
                    }
                    echo '<li><a href="' . esc_url( $term_child_link ) . '">' . $term_child->name . '</a>&nbsp;(' . $term_child->count . ')</li>';
                }
                echo '</ul>';
            }
    		echo '</li>';
        }
    echo '</ul>';
    }
    ?>

    のように書いているのですが、うまくいきません。

    $current_user->ID はログイン中のユーザーのIDを読み込めています。

    ‘author’ => $current_user->ID,
    では使い方を間違っているのでしょうか?

    どうぞ宜しくお願い致します。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • wp_list_categories は仕様として author パラメータがありません。get_terms も同様です。

    ちょっと面倒ですが次のような方法で表示することはできます(もっと簡単な方法があるかもしれませんが)。

    1. すべてのカテゴリーを get_categories で取得。または get_terms ですべてのタームを取得
    2. カテゴリー(ターム)ひとつずつ、以下を繰り返す
    3. ログイン中ユーザーのそのカテゴリー(ターム)の投稿一覧を get_posts で取得
    4. 取得できた投稿の数を count 関数で数える
    5. ゼロでなければカテゴリー名(ターム名)と数を表示
    トピック投稿者 kokorog

    (@kokorog)

    gblsm様

    考え方を教えて頂きありがとうございます!
    以下コードでできました。

    ・カテゴリ一覧

    <ul>
    <?php
    $args=array(
      'orderby' => 'name',
      'parent' => 0,
      );
    $categories=get_categories($args);
      foreach($categories as $category) {
      $myposts = get_posts( array(
    			'post_type' => 'post',
    			'category' => $category->term_id ,
    			'author' => $current_user->ID,
    			'numberposts' => -1
    		));
    		$mypostscount = count( $myposts );
    echo '<li><a href="' . esc_url( home_url( '/author/' .$current_user->display_name. '/?cat='. $category->term_id .'' ) ) . '">' . $category->name . '</a>&nbsp;(' . $mypostscount . ')</li>';
      }
    ?>
    </ul>

    ・特定タクソノミーのターム一覧(この場合items)

    <?php
    $taxonomy = 'items';
    $args = array(
        'parent' => 0,
        'pad_counts' => true,
        'hide_empty' => false
    );
    
    $terms = get_terms( $taxonomy , $args );
    if ( count( $terms ) != 0 ) {
    	echo '<ul>';
        foreach ( $terms as $term ) {
            $term = sanitize_term( $term, $taxonomy );
            $term_link = get_term_link( $term, $taxonomy );
            if ( is_wp_error( $term_link ) ) {
                continue;
            }
    		$myposts = get_posts( array(
    			'post_type' => 'post',
    			'taxonomy' => $taxonomy,
    			'term' => $term->slug,
    			'author' => $current_user->ID,
    			'numberposts' => -1
    		));
    		$mypostscount = count( $myposts );
            echo '<li><a href="' . esc_url( home_url( '/author/' .$current_user->display_name. '/?'.$taxonomy.'='. $term->slug .'' ) ) . '">' . $term->name . '</a>&nbsp;(' . $mypostscount . ')';
    
    		echo '</li>';
        }
    	echo '</ul>';
    }
    ?>

    ログイン中のユーザーの記事のみのカテゴリ(ターム)へのリンクの書き方が汎用性がなく無理やりでスマートではないですが、なんとかなってほっとしました。

    ありがとうございました。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「ログイン中のユーザーのカテゴリ一覧リンクを出したい」には新たに返信することはできません。