简体   繁体   中英

Function with the_author_posts_link returns link out of order in WordPress template

I've written a function for my WordPress theme to output the Author's name and post date. It runs inside the Loop.

$author = the_author_posts_link();
$date = get_the_date( 'j/n/y' );
echo '<span>Posted by </span><span>';
echo $author;
echo '</span><span> on</span><span> ' . $date . '</span>';

It is outputting this:

<a href="http://localhost/author/root/" title="Posts by root" rel="author">root</a>
<span>Posted by </span>
<span></span>
<span> on</span>
<span> 4/12/13</span>

The problem is that the Author link is output first and not where I expect it, which is causing formatting issues. No matter what I've tried, it returns the <a> before any of the other echoed code. No adjustments to the HTML seem to work - it looks like I have to handle this in PHP, but I don't know how.

Thanks in advance!

By default the_author_posts_link display out the link.

If you look at this link

http://core.trac.wordpress.org/browser/tags/3.7.1/src/wp-includes/author-template.php#L0

The function the_author_posts_link is echoing out the link instead of returning, So Change your code to:

$date = get_the_date( 'j/n/y' );
echo '<span>Posted by </span><span>';
the_author_posts_link();
echo '</span><span> on</span><span> ' . $date . '</span>';

Hope this works for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM