WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] <?the_author() ?> generates Wrong Author Name (5 posts)

  1. KatGirl
    Member
    Posted 7 years ago #

    This is my problem...

    In the main content section of a custom single7.php template is the Post Title <?php the_title(); ?> and the name of the author who wrote the post: <?php the_author(); ?> which sits inside the Loop.

    In the sidebar (custom sidebar-2.php template) I use this code to hightlight the current author's name and OTHER post links drawn from a SPECIFIC category (cat 7) for further reading.

    CODE:
    <?php query_posts('cat=7&author=' . $post->post_author . '&order=ASC&showposts=-1'); ?>
    <p class="subheading">All Titles by:</p>
    <?php if (have_posts()) : ?>
    <span><?php the_author(); ?></span>

    <ul>
    <?php while (have_posts()) : the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    <?php else: ?>
    <?php endif; ?>
    </ul>

    *************

    This is my problem. I've discovered that when Category 7 has but ONE author this code works beautifully.

    But if there are multiple authors (ie: more than one) in the same category (ie:Cat 7) then this code displays an alphabetical author name first (I guess because ASC was stipulated) NOT the RIGHT author name.

    In this case there were two (2) authors who have posted to Category 7.

    Karen Moning
    Michelle Pillow

    The owner of the post in question is Michelle (P), which was displayed appropriately in the content section but in the sidebar the author's name was Moning (M) which isn't correct. It should have been Pillow, as in keeping with the current author name.

    Interestingly enough, the Post Links in the sidebar were correct based off Single Page View: Pillows (P).

    Can someone PLEASE tell me how I can fix this code to show the current author's name in the sidebar (sorta "echoed"), along with a list of random post titles drawn from a specific category.

    Thanks

  2. Austin Matzko
    Member
    Posted 7 years ago #

    In this line:
    <?php query_posts('cat=7&author=' . $post->post_author

    I doubt $post->post_author is defined, because if you're in the sidebar you're probably outside the loop.

    You might have better success at setting a global author variable within the loop, then passing that to query_posts when you're outside the loop.

  3. KatGirl
    Member
    Posted 7 years ago #

    Hi Filosofo - sorry for the delay. Been working my little butt off.

    You might have better success at setting a global author variable within the loop, then passing that to query_posts when you're outside the loop.

    I'm a little confused. Could you please explain what you mean? Thanks for your help.

  4. Austin Matzko
    Member
    Posted 7 years ago #

    I'm assuming that the call to the sidebar comes after the single-post loop. If it comes before, then you'll have to do something else.

    While in the loop, set the single-post author's id to a variable; I've made up one and called it "$the_single_author":

    $the_single_author = $post->post_author;

    Then in the sidebar, first indicate that it's a global variable, then call query_posts:

    global $the_single_author;
    query_posts('cat=7&author=' . $the_single_author [etc.]

    That should return posts only of the right author and in category 7.

  5. KatGirl
    Member
    Posted 7 years ago #

    Thanks for the help, filosofo. I've give this a trial run and see how it performs.

Topic Closed

This topic has been closed to new replies.

About this Topic