WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How do I….list categories an author has wrote in on author.php

[Resolved] How do I….list categories an author has wrote in on author.php

  • scenario: a site with multiple authors. I click into a page about this author, and i want to see a list of categories this author has wrote in.

    how do i do this? im pretty stumped here. also how do i link to those categories?

Viewing 15 replies - 16 through 30 (of 34 total)
  • No problem, bear in mind, only using the ID will work, you’d need to join the users table to use the authors name in the query..

    I see. but no this is basically what I wanted it to do =)
    However I was trying to add the category description as well i tried doing this but it didnt work:

    <ul>
    	<?php foreach($categories as $category) : ?>
    	<li>
    		<a href="<?php echo get_category_link( $category->ID ); ?>" title="<?php echo $category->name ?>">
    			<?php echo $category->name ?> <?php echo $category->description ?>
            </a>
    	</li>
    	<?php endforeach; ?>
    </ul>

    Ok, try this..

    <?php
    $author = get_query_var('author');
    $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
    ");
    ?>
    <ul>
    	<?php foreach($categories as $category) : ?>
    	<li>
    		<a href="<?php echo get_category_link( $category->ID ); ?>" title="<?php echo $category->name ?>">
    			<?php echo $category->name.' '.$category->description; ?>
    		</a>
    	</li>
    	<?php endforeach; ?>
    </ul>

    ah sweeT! do you think this would work on single.php also?

    Yeah, but i’d imagine you’d need to change the author value, since they’ll not be any author=X query …

    ..perhaps..

    posts.post_author = '{$post->post_author}'

    thank you so much t310s =)

    No problem… 😉

    Queesy, thanks for asking that question. I needed that function too. However, the author page now shows the result twice 🙁 I assume it is because the author has two posts with different categories attached to them!?

    How can I avoid this?

    If you’ve placed the code inside any kind of loop, then i’d guess that’s why… because it’s being looped..

    I’d need to have a better idea of where you’re using it and the code surrounding it, but i’ll do what i can to help.

    Hi t31os_,

    true, it is inside the loop and I could place it outside to make it work. The template, Xplosive, has some kind of blue shading at the top of every template, which will go missing then. So maybe the question is how to keep that – I do not know what is controlling the shading. Style.css perhaps?

    The code:

    <?php get_header(); ?>
    <div class="entry">
    <!-- This sets the $curauth variable --><?php
    /*
    Template Name: Author Template
    */
    global $wp_query;
    $curauth = $wp_query->get_queried_object();
    $authid = $curauth->ID;
    ?>
    <?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post(); ?>
       			<div class="post" id="post-<?php the_ID(); ?>">
    
    <h2><?php the_author_posts_link(); ?></h2>
    <table border="0" cellspacing="2" cellpadding="2">
    <tbody>
    <tr>
    <td valign="top"><h5>Ekspertområder: </h5><?php
    //get all posts for an author, then collect all categories
    //for those posts, then display those categories
    $cat_array = array();
    $args=array(
     'author' => 4,
     '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);
    
    ?>
    <?php
    $categories = $wpdb->get_results("
    	SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug
    	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
    ");
    ?>
    <ul>
    	<?php foreach($categories as $category) : ?>
    	<li>
    		<a href="<?php echo get_category_link( $category->ID ); ?>" title="<?php echo $category->name ?>"><?php echo $category->name ?></a>
    	</li>
    	<?php endforeach; ?>
    <br /><h5>Website: </h5><a href="<?php echo $curauth->user_url; ?>" target="_blank">Website for <?php echo $curauth->user_firstname; ?> <?php echo $curauth->user_lastname; ?></a>
    <br /><h5>E-mail adresse:</h5> <a href="mailto:<?php echo $curauth->user_email; ?>">Send en e-mail til <?php echo $curauth->user_firstname; ?> <?php echo $curauth->user_lastname; ?></a>
    <br /><h5>Telefonnummer:</h5> <?php echo $curauth->aim; ?>
    <br /><h5>Facebook: </h5><a href="<?php echo $curauth->yim; ?>"><?php echo $curauth->yim; ?></a>
    <br /><h5>Andre kontaktlinks: </h5><a href="<?php echo $curauth->jabber; ?>"><?php echo $curauth->jabber; ?></a>
    </ul></td>
    <td width=200px valign="top"><?php author_image_tag($authid, 'align=right'); ?><div id="profilebox" style="min-height: <?php author_image_dimensions(author_image_path($authid, false, 'absolute'), 'height', true); ?>px;">
    </td>
    </tr>
    <tr>
    <td colspan="2" valign="top"><?php echo $curauth->user_description; ?></td>
    </tr>
    </tbody></table>
    </div>
    
      <?php endwhile; ?>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>
    
    	<?php else : ?>
    		<div class="post">
    		<h2 class="center search">Ups, det kunne vi ikke finde!</h2>
    		<p class="center">Du leder efter noget, der ikke findes her. Beklager!</p>
            </div>
    
    	<?php endif; ?>
    	</div>
        <div>
    <?php include (TEMPLATEPATH . '/topbar.php'); ?>
    <div>
    <?php include (TEMPLATEPATH . '/sidebar1.php'); ?>
    <?php include (TEMPLATEPATH . '/sidebar2.php'); ?>
    </div>
    </div>
    <br clear="all" />
    <?php get_footer(); ?>
    </div>
    </body>
    </html>
    Nikolas
    Member

    @kordellas

    This script it’s great. What I was looking for!

    Can you please help me and make it to show only the child categories instead of all categories?

    I read something about depth but i did not manage to make it right…

    Think it’s just a code placement issue by looking at the above, what file are you placing this in, and is this bottom code required, shouldn’t that already be in the footer file?

    </div>
    </body>
    </html>

    I have removed the bottom code now without any harm done 🙂

    The file the code is placed in is in profile-pic/author.php (a Profile Pic plugin).

    http://wordpress.pastebin.ca/1689688

    Fixed a few invalid areas, and commented out code not being used…

    Not entirely sure what your intended result is, but try the above and report back please..

    itsbarry
    Participant

    @itsbarry

    Hello,

    Great thread. I’d like to do something a little simpler (I hope!)

    I am modifying my author.php template.

    I want to show all posts by an author, but I want to restrict results to a single category. Always. So I’m hard-coding the category. However I have multiple authors so I have to pass the author id.

    My code:

    <?php
    // housekeeping to get the author's name
    if(isset($_GET['author_name'])) :
        // NOTE: 2.0 bug requires: get_userdatabylogin(get_the_author_login());
        $curauth = get_userdatabylogin($author_name);
    else :
        $curauth = get_userdata(intval($author));
    endif;
    ?>
    
    <?php
    // I just want to show blog posts in cat 6
    query_posts('cat=6&author=$curauth');
    ?>

    This doesn’t work. I think my syntax could be wrong, but please let me know if I’m on the right track here.

    thanks,

    Barry

Viewing 15 replies - 16 through 30 (of 34 total)
  • The topic ‘[Resolved] How do I….list categories an author has wrote in on author.php’ is closed to new replies.