最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Output author and description if description is not empty

programmeradmin1浏览0评论

Is it possible to only display the author's name and description (aka bio) if the description contains text?

This code doesn't work (it doesn't return the name or description) but hopefully it can be edited to accomplish this goal:

<?php
$authorDesc = the_author_meta($post->ID, 'description', true);
if (!empty($authorDesc)) {
?>
<em>by <?php the_author(); ?></em>
<span><?php the_author_meta('description'); ?></span>
<?php } ?>

Is it possible to only display the author's name and description (aka bio) if the description contains text?

This code doesn't work (it doesn't return the name or description) but hopefully it can be edited to accomplish this goal:

<?php
$authorDesc = the_author_meta($post->ID, 'description', true);
if (!empty($authorDesc)) {
?>
<em>by <?php the_author(); ?></em>
<span><?php the_author_meta('description'); ?></span>
<?php } ?>
Share Improve this question asked Mar 4, 2011 at 23:45 RyanRyan 5152 gold badges11 silver badges22 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2
<?php
$authordesc = get_the_author_meta( 'description' );

if ( ! empty ( $authordesc ) )
{
?>
    <a href="<?php
    echo get_author_posts_url( get_the_author_meta( 'id' ) );
    ?>"><?php
    the_author();
    ?></a>
    <?php
    echo wpautop( $authordesc );
}

First, you need to use get_the_author_meta instead of the_autho_meta to give a vakue to $authorDesc (get_the_author_meta returns the value, the_author_meta displays it).

Secondly, you need to use user_description as the argument for both functions instead of description.

Hope it works.

EDIT - Here is the documentation to the_author_meta for more info. EDIT 2 - You also don't need to declare $post->ID as the first parameter for the_author_meta. Are you using it inside the loop?

This worked for me (10 years later lol)

<?php // Get author's bio
$user_description = get_the_author_meta( 'user_description', $post->post_author ); ?>
    
<?php if ( ! empty( $user_description ) ): ?>
<p class="author-desc"> 
<?php the_author_meta('description') ?>
</p>
<?php endif; ?> 
发布评论

评论列表(0)

  1. 暂无评论