web-dev-qa-db-fra.com

Liste des catégories pour l'auteur: Fonction list_categories à l'intérieur de la fonction list_authors

J'essaie de créer une page "contributeurs" contenant une liste des auteurs et les catégories qu'ils ont publiées.

Je peux utiliser ce code pour le faire pour un seul auteur sur une seule page de message:

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

Mais quand j'essaie de brancher ce bit dans la liste de mes auteurs (pour que chaque auteur de la liste ait sa catégorie respective affichée), la liste des catégories s'affiche en haut de la page dans le div où il est censé s'afficher, il est simplement écrit "tableau". Je pense que c'est une question de syntaxe car je suis un peu un PHP newb.

Voici la fonction de la liste des auteurs:

//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;
}

Toute idée ou idée très appréciée, merci!

2
j-man86

Cela semblait intéressant, alors voici ma version. Pas sûr de get_user_by(), devrait être un moyen plus robuste d'obtenir des objets pour les auteurs.

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

Vous essayez de déboguer votre script PHP via la communauté. La meilleure chose à faire avec les problèmes PHP est de trouver la cause première par vous-même afin d'apprendre le langage PHP. Apprendre est le processus consistant à faire des erreurs et à comprendre ensuite comment les prévenir. C'est quelque chose que vous ne pouvez faire que par vous-même, ce n'est pas une communauté qui peut le faire à la place de vous.

Par exemple, pour en savoir plus sur les tableaux, consultez la documentation PHP: Arrays .

En plus de l'utilisation fausse de PHP, vous pouvez également utiliser de fausses informations HTML. Mais les problèmes HTML sont normalement plus faciles à trouver, commencez donc par l’un puis par l’autre. Commencez par PHP dans votre cas :).

1
hakre