web-dev-qa-db-ja.com

作者のリストカテゴリ:list_authors関数の中のlist_categories関数

私は著者のリストと彼らが投稿したカテゴリーがある「貢献者」ページを作成しようとしています。

私は単一の投稿ページの単一の著者のためにこれをするためにこのコードを使うことができます:

    <?php
    $cat_array = array();
    $args=array(
     'author' => get_the_author_meta('id'),
     'showposts'=>-1,
     'caller_get_posts'=>1
    );
    $author_posts = get_posts($args);
    if( $author_posts ) {
      foreach ($author_posts as $author_post ) {
        foreach(get_the_category($author_post->ID) as $category) {
          $cat_array[$category->term_id] =  $category->term_id;
        }
      }
    }

    $cat_ids = implode(',', $cat_array);
    $output = strtr( wp_list_categories( 'include='.$cat_ids.'&title_li=&style=none&echo=0' ), array( '<br />' => ' / ' ) );
    echo preg_replace( '@\s/\s\n$@', '', $output );
    ?>

しかし、それを私のリストの作者の機能に追加しようとすると(リストの各作者がそれぞれのカテゴリーを表示するように)カテゴリーのリストはページの上部に表示されます(コードを配置したdivではなく)そして表示されるはずのdivでは、単に「配列」と表示されます。私は一種のPHP newbなので、これは構文の問題だと思います。

これがlist authors関数です。

//My List Authors Function
function my_list_authors($args = '') {
    global $wpdb;
    global $wp_query;
$author = get_query_var('author');
function authorCats() {
$categories = $wpdb->get_results("
    SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.description
    FROM $wpdb->posts as posts
    LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
    LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
    LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
    WHERE 1=1 AND (
        posts.post_status = 'publish' AND
        posts.post_author = '$author' AND
        tax.taxonomy = 'category' )
    ORDER BY terms.name ASC
");
foreach($categories as $category) :
echo '<li>
        <a href="'.get_category_link( $category->ID ).'" title="'.$category->name.'">
            '.$category->name.'
        </a>
    </li>';
endforeach;
}
    $defaults = array(
        'optioncount' => false, 'exclude_admin' => true,
        'show_fullname' => false, 'hide_empty' => true,
        'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
        'style' => 'list', 'html' => true
    );

    $r = wp_parse_args( $args, $defaults );
    extract($r, EXTR_SKIP);
    $return = '';

    /** @todo Move select to get_authors(). */
    $users = get_users_of_blog();
    $author_ids = array();
    foreach ( (array) $users as $user )
        $author_ids[] = $user->user_id;
    if ( count($author_ids) > 0  ) {
        $author_ids = implode(',', $author_ids );
        $authors = $wpdb->get_results( "SELECT ID, user_nicename from $wpdb->users WHERE ID IN($author_ids) " . ($exclude_admin ? "AND user_login <> 'admin' " : '') . "ORDER BY display_name" );
    } else {
        $authors = array();
    }

    $author_count = array();
    foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row )
        $author_count[$row->post_author] = $row->count;

    foreach ( (array) $authors as $author ) {

        $link = '';

        $author = get_userdata( $author->ID );
        $posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
        $name = $author->display_name;

        if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
            $name = "$author->first_name $author->last_name";

        if( !$html ) {
            if ( $posts == 0 ) {
                if ( ! $hide_empty )
                    $return .= $name . ', ';
            } else
                $return .= $name . ', ';

            // No need to go further to process HTML.
            continue;
        }

        $authorAvatar = get_avatar($author->ID);

        if ( !($posts == 0 && $hide_empty) && 'list' == $style )
            $return .= '
            <div class="authorBox">
                '.$authorAvatar.'
                <table>
                    <tr>
                        <td class="authorLabel">Founder & Editor:</td>
                        <td class="authorData"><a href="#">'.$author->nickname.'</a></td>
                    </tr>

                    <tr>
                        <td class="authorLabel">Location:</td>
                        <td class="authorData">'. $author->location .'</td>
                    </tr>

                    <tr>
                        <td class="authorLabel">Industry:</td>
                        <td class="authorData">Advertising</td>
                    </tr>';
            if ( $author->Twitter != '' ) {
            $return .= 
                    '
                    <tr>
                        <td class="authorLabel">Website:</td>
                        <td class="authorData"><a href="#">'.$author->user_url.'</a></td>
                    </tr>

                    <tr class="last">
                        <td class="authorLabel">Twitter:</td>
                        <td class="authorData"><a href="#">'.$author->Twitter.'</a></td>
                    </tr>';
            } else {
            $return .= '
                    <tr class="last">
                        <td class="authorLabel">Website:</td>
                        <td class="authorData"><a href="#">'.$author->user_url.'</a></td>
                    </tr>';
            }
            $return .= '
                </table>

                <div class="bioBox">
                    <ul>
                        <li class="bioTab"><a>Bio</a> <span></span></li>
                        <li class="thinkingTab"><a>Thinking About</a> <span></span></li>
                        <li><a>Articles</a> <span></span></li>  
                        <li><a>Reactions</a> <span></span></li>
                    </ul>
                </div>
                        <div class="authorBio tab"> 
                            <p class="center">'
                            .$author->description.  
                            '</p>
                        </div>

                        <div class="authorThink tab"> 
                            <p class="center">';
            $return .= 
$cat_array = array();
$args=array(
 'author' => get_the_author_meta('id'),
 'showposts'=>-1,
 'caller_get_posts'=>1
);
$author_posts = get_posts($args);
if( $author_posts ) {
  foreach ($author_posts as $author_post ) {
    foreach(get_the_category($author_post->ID) as $category) {
      $cat_array[$category->term_id] =  $category->term_id;
    }
  }
}

$cat_ids = implode(',', $cat_array);
$output = strtr( wp_list_categories( 'include='.$cat_ids.'&title_li=&style=none&echo=0' ), array( '<br />' => ' / ' ) );
echo preg_replace( '@\s/\s\n$@', '', $output );
            $return .=          '</p>
                        </div>
            </div>';
    }

    $return = trim($return, ', ');

    if ( ! $echo )
        return $return;
    echo $return;
}

非常に感謝されているどんな洞察力やアイデア、ありがとう!

2
j-man86

おもしろそうだったので、こちらが私のバージョンです。 get_user_by()について確信が持てない、作者のためのオブジェクトを取得するためのより堅牢な方法であるべきです。

function my_list_authors() {

    $authors = wp_list_authors( array(
    'exclude_admin' => false,
    'html' => false,
    'echo' => false
    ) );

    $authors = explode( ',', $authors );

    echo '<ul>';

    foreach ( $authors as $author ) {

    $author = get_user_by( 'login', $author );
    $link = get_author_link( false, $author->ID );
    echo "<li><a href='{$link}'>{$author->display_name}</a><ul>";

    $posts = get_posts( array(
        'author' => $author->ID,
        'numberposts' => -1
    ) );

    $categories = array();

    foreach ( $posts as $post )
        foreach( get_the_category( $post->ID ) as $category )
        $categories[$category->term_id] =  $category->term_id;

    $output = wp_list_categories( array(
        'include' => $categories,
        'title_li' => '',
        'echo' => false
        ) );

    echo $output . '</ul></li>';
    }

    echo '</ul>';
}
2
Rarst

あなたはコミュニティを通してPHPスクリプトをここでデバッグしようとしています。あなたがPHPの問題であなたができる最善のことはあなたがPHPを学ぶようにあなた自身で根本原因を見つけることです。学習は、間違いを犯し、それから将来それらを防ぐ方法を理解するプロセスです。これは、あなたが自分でしかできないことです。あなたの代わりにコミュニティができることは何もありません。

たとえば、配列の詳細については、PHPのドキュメントを参照してください。 配列

PHPの誤った使い方の次に、HTMLも誤った使い方をするかもしれません。しかし、HTMLの問題は通常見つけやすいので、一方から始めて他方から始めてください。あなたのケースではPHPから始めてください:).

1
hakre