3

I am trying to make a "contributors" page where there is a list of authors and the categories they have posted in.

I am able to use this code to do this for a single author on a single post page:

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

But when I try and plug that bit into my list authors function (so that each author in the list has their respective categories displayed) The list of categories displays at the top of the page (not in the div where I put the code) and in the div where it's supposed to display, it simply says "array". I think this is a question of syntax as I am kind of a PHP newb.

Here is the list authors function:

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

Any insight or ideas much appreciated, thanks!

2 Answers 2

3

Seemed interesting, so here is my version. Not sure about get_user_by(), should be more robust way to get objects for authors.

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>';
}
1

You try to debug your PHP script here via the community. The best thing you can do with PHP issues is to find the root-cause on your own so you learn PHP. Learning is the process of making errors and then to understand how to prevent them in the future. This is something that you can only do on your own, it's nothing a community can do instead of you.

For example, to learn more about arrays, please visit the PHP documentation: Arrays.

Next to false usage of PHP, you might make false use of HTML as well. But the HTML problems are normally easier to find, so just start with one and then with the other. Start with PHP in your case :).

4
  • 1
    My understanding is that the community exists to connect humans with humans, rather than humans with static codex/manual pages. Reading this manual doesn't do a quarter of what a well thought out explanation from an experienced PHP developer would, which is why I asked the question here. I wouldn't be asking the question here if I hadn't already spent hours upon hours trying to find the root cause on my own- part of the learning process. Thanks. Commented Aug 24, 2010 at 12:45
  • 1
    I know learning is hard. - If it says "array()" somewhere in your output you print/echo the whole array variable out somewhere. Locate that place because it's nothing you want to do. - If it outputs stuff prior where you want to have it output to, ensure that all expressions you make use of do not output anything. E.g. locate the expression that does output stuff (maybe a wp template tag?) and replace it with something that does not do any output.
    – hakre
    Commented Aug 24, 2010 at 13:07
  • And btw, you just connected humans here with your static broken code. So if you feel offended by sending you a manual link and you think that's too unpersonal, then please take a look for a PHP usergroup near your location. There you can meet up with persons face to face and share about the topic in a non static way.
    – hakre
    Commented Aug 24, 2010 at 13:09
  • P.S. Learn what a function is and how to write it. Looks like you defined a function inside a function which is possible in PHP but in your case it might be a sign that you mixed something completely. Too much use of Copy & Paste?
    – hakre
    Commented Aug 24, 2010 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.