【问题标题】:get_the_author_meta() not working on wpget_the_author_meta() 不适用于 wp
【发布时间】:2017-06-05 18:26:46
【问题描述】:

我对标准 wp 函数 get_the_author_meta() 有一些问题。我在我的项目中使用它来显示作者的社交链接,但我总是得到一个空变量。

这是我在文章页面中的代码:

    $author_name = get_the_author();//not empty
    $author_info = get_the_author_meta('description');//not empty
    $author_website = get_the_author_meta('url');//empty
    
    $author_facebook = get_the_author_meta('facebook');//empty
    $author_twitter = get_the_author_meta('twitter');//empty
    $author_google = get_the_author_meta('google');//empty
    $author_linkedin = get_the_author_meta('linkedin','7');//also empty whit this desperate attempt
    $author_instagram = get_the_author_meta('instagram',7);//also empty whit this desperate attempt

我测试了具有所有社交链接的作者的文章。 我没有 php 没有 js 错误。 我安装了 cimy 用户额外字段。

【问题讨论】:

  • 为什么不先转储作者数据,看看这些字段是否存在。
  • var_dump(get_the_author_meta());//string(0) "" var_dump(get_the_author_meta(7));//string(0) "" var_dump(get_the_author_meta('7'));/ /string(0) "" 最奇怪的是所有这些字段都存在并且在数据库中不为空。
  • 某事告诉我您正在使用插件将这些社交链接添加给作者。根据 Codex,返回空字符串的字段不是有效字段。 codex.wordpress.org/Function_Reference/the_author_meta
  • 是的,我加了。您可以在 DB 上找到确切的元数据(facebook、Twitter excetera)。

标签: php wordpress


【解决方案1】:

首先这里有一个额外的逗号 $author_facebook = get_the_author_meta( 'facebook', ); //空

我猜这是复制粘贴中的错误,否则您会收到一个非常具体的“意外令牌”错误。除了这个例外,您的代码应该可以工作。您收到此错误的原因可能有两个:

  1. 您用于添加用户元数据的插件无法正常工作。
  2. 页面/帖子的实际作者没有正确设置元数据。

这段代码对我有用,试试这个只是为了测试:

$user_id = get_the_author_meta( 'ID' );
add_user_meta( $user_id, 'facebook', 'https://facebook.com' );

var_dump( $user_id );
var_dump( get_the_author_meta( 'facebook' ) );

// Output: string(20) "https://facebook.com"

您之前提到您可以在usermeta下看到DB上的社交媒体链接,所以我的猜测是帖子/页面的当前作者没有设置社交媒体链接。在代码的 var_dumps 上,您将看到用户 ID。在数据库中查找具有该用户 ID 的表 usermeta 的所有条目,您会注意到该特定用户没有自定义元。

【讨论】:

  • 非常感谢您的解释。现在我终于得到了完整的过程。我还有一个额外的错误: - 我将“google”替换为“googleplus
猜你喜欢
  • 2018-01-01
  • 2018-01-08
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 1970-01-01
  • 2016-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多