WordPress 作者头像调用方法

admin123 2019-2-15 5966

WordPress 作者头像调用方法

WordPress作者头像调用有很多种方法,整理如下:

1. 在文章页面调用作者头像, 非常简单, 一句话就可以实现:

<?php echo get_avatar( get_the_author_email(), 60 ); ?>

2. 在作者列表页面调用作者头像, 方法和页面有点不同, 不过也不麻烦, 具体代码如下:

在author-template.php模板里添加下面的代码即可:

$author_email = get_the_author_email();
$avatar =  get_avatar($author->ID ,32);

然后将

$link = ‘<a href=”‘ . get_author_posts_url($author->ID, $author->user_nicename) . ‘” title=”‘ . esc_attr( sprintf(__(“Posts by %s”), $author->display_name) ) . ‘”>’ .$name. ‘</a>’;

改成:

$link = ‘<a href=”‘ . get_author_posts_url($author->ID, $author->user_nicename) . ‘” title=”‘ . esc_attr( sprintf(__(“Posts by %s”), $author->display_name) ) . ‘”>’ .$avatar . ‘</a>’;

即可.

3. 作者页面的头像调用又有点特别, 需要做特别处理, 具体实现方法如下:

<?php
// Determine which gravatar to use for the user
$GLOBALS[‘defaultgravatar’] = $template_path . ‘/images/avatar.gif’;
$email = $curauth->user_email;
$grav_url = “http://www.gravatar.com/avatar.php?gravatar_id=”.md5($email). “&default=”.urlencode($GLOBALS[‘defaultgravatar’] ).”&size=60″;
?>
<img src=”<?php echo $grav_url; ?>” width=”60″ height=”60″ alt=”” />


最新回复 (0)
全部楼主
返回