web-development-kb-pt.site

Listar categorias para autor: list_categories function inside list_authors function

Estou tentando criar uma página de "contribuidores" onde há uma lista de autores e as categorias nas quais eles postaram.

Eu posso usar este código para fazer isso para um único autor em uma única página de postagem:

    <?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\[email protected]', '', $output );
    ?>

Mas quando eu tento plugar esse bit na minha lista de autores (para que cada autor da lista tenha suas respectivas categorias exibidas) A lista de categorias é exibida no topo da página (não na div onde eu coloquei o código) e no div onde deveria ser exibido, ele simplesmente diz "array". Eu acho que isso é uma questão de sintaxe, já que eu sou uma espécie de PHP newb.

Aqui está a lista de autores:

//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\[email protected]', '', $output );
            $return .=          '</p>
                        </div>
            </div>';
    }

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

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

Qualquer ideia ou ideias muito apreciadas, obrigado!

2
j-man86

Parecia interessante, então aqui está a minha versão. Não tenho certeza sobre get_user_by(), deve ser uma maneira mais robusta de obter objetos para autores.

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

Você tenta depurar seu script PHP aqui através da comunidade. A melhor coisa que você pode fazer com o PHP issues é encontrar a causa raiz por conta própria para aprender PHP. Aprender é o processo de cometer erros e depois entender como evitá-los no futuro. Isso é algo que você só pode fazer sozinho, não é nada que uma comunidade possa fazer em vez de você.

Por exemplo, para saber mais sobre arrays, visite a documentação PHP: Arrays .

Além do uso falso do PHP, você pode fazer uso falso de HTML também. Mas os problemas de HTML são normalmente mais fáceis de encontrar, então comece com um e depois com o outro. Comece com PHP no seu caso :).

1
hakre