• Pete

    (@perthmetro)


    I have this piece of code…

    echo "<a href='".get_permalink($post_id)."' title='". $p->post_title ."'>" . $p->post_title . "</a> ";

    Instead of displaying the post title I’d like it to display the post’s author’s name (but still link to the post)

    Thanks heaps if you can help

    Pete

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator t-p

    (@t-p)

    you mean something like:

    <p>This post was written by <?php the_author(); ?></p>

    Thread Starter Pete

    (@perthmetro)

    no, that doesn’t work with the above format

    Moderator bcworkz

    (@bcworkz)

    You should be able to replace the second $p->post_title with get_the_author(). Assuming this is inside a loop where $p->the_post() is called. Otherwise we’ll need more context for where this code is and where $p is coming from.

    Thread Starter Pete

    (@perthmetro)

    Doesn’t quite work, here is more of the code…

    echo "<ul>";
    if ($favorite_post_ids):
    	$c = 0;
    	$favorite_post_ids = array_reverse($favorite_post_ids);
        foreach ($favorite_post_ids as $post_id) {
        	if ($c++ == $limit) break;
            $p = get_post($post_id);
            echo "<li>";
            echo "#<a href='".get_permalink($post_id)."' title='". $p->post_title ."'>" . get_the_author() . "</a> ";
            echo "</li>";
        }
    else:
        echo "<li>";
        echo "Your favorites will be listed here.";
        echo "</li>";
    endif;
    echo "</ul>";

    Moderator bcworkz

    (@bcworkz)

    Now replace get_the_author() with get_userdata($p->post_author)->user_login

    Instead of user_login you can substitute user_nicename or display_name. If you want real names or nickname from user meta, use
    get_user_meta($p->post_author, 'nickname', true)

    Then instead of nickname you could substitute user_firstname or user_lastname. You can also concatenate calls for each name together to build more complex identification strings, to the point it gets silly, such as “John Doe, who goes by JD, but is known here as jdoe” ??

    Thread Starter Pete

    (@perthmetro)

    Brilliant, thanks… it “workz” a treat. ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to echo the author's name with a link to the author's post’ is closed to new replies.