WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How do I....list categories an author has wrote in on author.php (35 posts)

  1. Kevin Kwok
    Member
    Posted 4 years ago #

    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?

  2. MichaelH
    Member
    Posted 4 years ago #

    Since you are using Author Templates you just need to create an array of all the categories from get_the_category(), as you loop through a given author's posts.

    Related:
    http://php.net/manual/en/language.types.array.php
    Function_Reference/get_the_category

  3. Kevin Kwok
    Member
    Posted 4 years ago #

    i still cant seem to come up with a way to make this work=\

  4. MichaelH
    Member
    Posted 4 years ago #

    Well at least start here: http://wordpress.org/support/topic/327359

  5. Kevin Kwok
    Member
    Posted 4 years ago #

    @michaelH
    sorry about the duplicate i couldnt find the older one, thank you for closing it.

    =======================================================================

    Hello!
    my website: ampersandbox.net/?author=2

    Basically on every author's page I would like to display a list on the right side of categories the current author has contributed to (has written a post in)

    I'm not sure how to go about this but I started trying to write some code I came up with this code but for some reason it only displays one category which happens to be category 2. (check column on right labeled CATEGORIES)

    <?php
    //Gets category and author info
      global $wp_query;
    $cats = get_the_category();
      $postAuthor = $wp_query->post->post_author;
    $tempQuery = $wp_query;
    // related category posts
      $catlist = "";
      forEach( $cats as $c ) {
      if( $catlist != "" ) { $catlist .= ","; }
      $catlist .= $c->name;
      $catlist .= $c->description;
      }
      $newQuery = "posts_per_page=5&cat=" . $catlist;
      query_posts( $newQuery );
    $categoryPosts = "";
      $count = 0;
      ?>
    <?php echo $catname .' '. $catlist; ?>
  6. Kevin Kwok
    Member
    Posted 4 years ago #

    still wasnt able to achieve this not much of a php coder =\

  7. MichaelH
    Member
    Posted 4 years ago #

    <?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);
    wp_list_categories('include='.$cat_ids.'&title_li=Author Categories');
    ?>
  8. Kevin Kwok
    Member
    Posted 4 years ago #

    thanks michael! thats definitely doing something however it looks like its just listing all the categories i have not specific ones the author has posts in. but i will keep working at it thank you!

    ampersandbox.net/?author=2

  9. Kevin Kwok
    Member
    Posted 4 years ago #

    open to solutions or alternatives im pretty stumped.

  10. Mark / t31os
    Moderator
    Posted 4 years ago #

    Just create some links..
    http://ampersandbox.net/?author=2&cat=1 - working example link

    WordPress will sort out getting the posts that match for you..

  11. Kevin Kwok
    Member
    Posted 4 years ago #

    WOW i did not know you could do that thank you so much I will fool around with your idea and see if i cant come up with something thank you =)

  12. Kevin Kwok
    Member
    Posted 4 years ago #

    I grabbed this code and pieced some stuff together...but it looks like it does the same thing as Michael gave me

    http://wordpress.pastebin.ca/1660678

    It basically displays all categories, What I want to do is exclude the categories that an author has not posted in...

  13. Mark / t31os
    Moderator
    Posted 4 years ago #

    There's no need for any of that, what do you currently have in your author.php file? (aka the author template).

    Template files (index.php, archive.php etc..) already know what to do when query vars exist, the only time they can't intercept and use those queried variables is when you over-ride the query, ie. you're setting the query_posts line.

    If you can show me the code being used in the author template file, i can suggest changes or additions to allow the file to do what it should actually already be doing.

    A query like..

    query_posts('author=1&cat=1');

    should provide a list of posts in that category and by that author only, there should be nothing else required other then to ensure you are querying that page in a fashion that places the correct query vars into the query.

  14. Kevin Kwok
    Member
    Posted 4 years ago #

    Hello t31os_!

    Thank you for replying again. What you showed/told me before about adding ?author=1&cat=1 works great however I basically need to generate a list of categories on the author.php template

    each list should be different for each author because my goal is to have it only lists categories the author is posting in. currently the code i m using from michael and the other source lists all the categories even if the author has no posts. I basically need to exclude a category if an author has zero posts in.

    Example: kevin has written a post in category one and two and john has written a post only in category one

    therefore on kevins page it lists: one and two
    on johns page it lists: one

    here is the code michael provided for me:

    <?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);
    wp_list_categories('include='.$cat_ids.'&title_li=Author Categories');
    ?>
  15. Mark / t31os
    Moderator
    Posted 4 years ago #

    Ok i think i misunderstood before.

    However, you could use this method, since it's a little faster..

    <?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 = '4' 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; ?>
    </ul>

    If it's a small site and you've not got thousands of posts, you'll likely not notice a difference though... but for the sake of following through ... ;)

    If you want to find out the which author has been queried then it's a simple case of..

    $author = get_query_var('author'); // Queried by ID

    or

    $author = get_query_var('author_name'); // Queried by name

    Add the variable into the other code, and you are essentially querying for categories for the current author being queried..

    This line..

    posts.post_author = '4'

    ..would become..

    posts.post_author = '$author'
  16. Kevin Kwok
    Member
    Posted 4 years ago #

    WOW i love you for this! thank you so much it works perfectly! exactly what i wanted. you are truly amazing =) thank you so much!!!!!

  17. Mark / t31os
    Moderator
    Posted 4 years ago #

    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..

  18. Kevin Kwok
    Member
    Posted 4 years ago #

    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>
  19. Mark / t31os
    Moderator
    Posted 4 years ago #

    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>
  20. Kevin Kwok
    Member
    Posted 4 years ago #

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

  21. Mark / t31os
    Moderator
    Posted 4 years ago #

    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}'
  22. Kevin Kwok
    Member
    Posted 4 years ago #

    thank you so much t310s =)

  23. Mark / t31os
    Moderator
    Posted 4 years ago #

    No problem... ;)

  24. henrik@worsoe.eu
    Member
    Posted 4 years ago #

    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?

  25. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

  26. henrik@worsoe.eu
    Member
    Posted 4 years ago #

    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>
  27. Nikolas
    Member
    Posted 4 years ago #

    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...

  28. Mark / t31os
    Moderator
    Posted 4 years ago #

    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>
  29. henrik@worsoe.eu
    Member
    Posted 4 years ago #

    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).

  30. Mark / t31os
    Moderator
    Posted 4 years ago #

    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..

Topic Closed

This topic has been closed to new replies.

About this Topic