好的,所以我在WordPress编辑配置文件字段中添加了一个自定义字段,如果贡献者不使用Gravatar,则可以在其中添加自定义图像。现在我试图写一个如果其他语句,这就是我拥有的

<?
    $hasauthorpic = (the_author_meta('author_pic'));
    if (function_exists('get_avatar')) { echo get_avatar( get_the_author_email(), '80' );}
    else {echo '?><img class="avatar avatar-80 photo"><? $hasauthorpic ?></img><? ';}
?>

我想尝试做的是,如果用户确实有Gravatar使用它,除非他们指定了指向Profile Pic的链接。或者即使用户拥有Gravatar,futor_pic也变得更高的优先级。

编辑:

<?
$authorpic = the_author_meta('author_pic');
$gravatar = get_avatar( get_the_author_email(), '80' );
    if ($authorpic); 
    elseif (function_exists('get_avatar')) 
            echo ($gravatar); 
?>

好的,所以我尝试了下面的代码,但这还没有用。也许是因为我将其放入单个whate.php文件中。以上是我设法得到的 <img> 标签,但以后很容易。我读过的一件事是,你不能放 truethe_author_meta 所以我需要帮助。

如果您可以提出一个代码将Gravatar的东西钩住,那就生了。换句话说,如果您有一个可以放在我的函数中的代码。字段名称是 author_pic

更新: 这是我的最后一篇文章,其中包括下面提供的代码

<?php
$authorpic = get_the_author_meta('author_pic');
$imgtagbeg = ('<img style="height:80px; width:80px" src="');
$imgtagend = ('"/>');
if ($authorpic)
    echo $imgtagbeg,$authorpic,$imgtagend; 
else
    echo get_avatar( get_the_author_email(), '80' ); 
?>
有帮助吗?

解决方案

您需要使用get_the_author_meta()而不是the_author_meta()

<?php
$authorpic = get_the_author_meta('author_pic');
if ($authorpic)
    echo $authorpic; 
else
    echo get_avatar( get_the_author_email(), '80' ); 
?>

其他提示

您的 if 将始终返回真实并绕过您的自定义作者图片。

在自定义字段上运行IF。如果返回true,请在使用自定义字段作者图片的get_avatar中添加过滤器。

if ( the_author_meta('author_pic') ) {
add_filter( 'get_avatar', 'your_custom_author_pic_function' );
}

我最近发现了一个甜美的新插件 简单的本地化身 那将完全按照您的要求做。一探究竟!

如果我理解正确,您不希望人们上传自定义的化身,但是您确实希望人们能够链接到自定义的化身吗? (并且,如果没有,则默认的头像或Gravatar启动),如果是这样,本文可能会适合您的需求 http://www.billerickson.net/wordpress-custom-avatar/

许可以下: CC-BY-SA归因
scroll top