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

    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

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    Hi Barry,

    It may not be immediately obvious, but i've spotted your problem.. just here..

    query_posts('cat=6&author=$curauth');

    You can't use variables inside single quotes, they get treated as literal values.

    The alternatives
    Concatate the string:

    query_posts('cat=6&author=' . $curauth);

    Switch to double quotes so you can use variables:

    query_posts("cat=6&author=$curauth");

    First method will be faster ... but you'll not likely notice the difference. Use whichever suits you, they'll both essentially do the same..

    Hope that helps.. :)

  3. itsbarry
    Member
    Posted 4 years ago #

    Hi t31os_,

    I was hoping you'd reply!

    Actually I figured it out. I ended up going with this:

    query_posts('cat=6&author=$curauth->ID')

    Also I found this "in the woods" article valuable when I was putting this project together.

    thanks much!

  4. itsbarry
    Member
    Posted 4 years ago #

    Update:

    My solution did not work after all.

    t310s's first approach threw an error

    So what worked for me was his second suggestion, using double quotes.

    Thanks again

    Barry

  5. Gawd
    Member
    Posted 4 years ago #

    Hi,

    I am looking to do something along the same lines are the people here also.

    In my sidebar I have author links listed as my headings.
    When someone clicks the link to go to the author page, I'd like a listing to be displayed under there name in the sidebar with all the categories they've posted in.

    I am not sure how to get that author specific category listing.

Topic Closed

This topic has been closed to new replies.

About this Topic