首页 > 解决方案 > 高级自定义字段、木材和检索用户名

问题描述

我正在尝试找到一种方法在帖子下方显示每个用户的名称,他们将被列为作者和贡献者,每个名称都将链接到他们的个人资料,并且在他们的个人资料中还有字段(来自最终会员插件)这是他们的加密钱包公共地址,这是他们可以用来接收加密货币捐赠的一种方式,因为他们对博客的贡献。我在下面的草图中总结了所有内容:

草图(或截图)

问题是我正在使用Gantry5框架,直到最近我才发现Gantry使用了它自己的一种编程语言,叫做Timber,这也让我想起了一些关于最终幻想XVIII的记忆,好吧,无论如何,我面临的问题这是我无法检索用户名。

该文件名为“content-single.html.twig”,位于以下结构“themes/g5_helium/views/partials/”中。使用 Gantry5 Helium 主题。但由于我创建了一个自定义覆盖,它位于:“themes/g5_helium/custom/views/partials/”</p>

我在这部分之后编辑该区域:

{# Begin Page Content #}
                {{ post.paged_content|raw }}

                {{ function('wp_link_pages', {'before': '<div class="page-links" itemprop="pagination"><ul class="pagination-list">', 'after': '</ul></div>', 'link_before': '<span class="page-number page-numbers">', 'link_after': '</span>', 'echo': 0}) }}
                {# End Page Content #}

这是我在 Gantry 社区的一个人帮助我之后使用的:

{% set post_id = function('get_the_ID') %}
{% set post = wordpress.call('Timber::get_post', post_id) %}
{{ post.meta('autor')|raw }}                
{{ post.meta('narrador')|raw }}

问题是无论我为用户数组还是用户对象设置数据格式,我都会得到“数组”作为输出。只有当我设置用户 ID 时,我才会得到 ID 号,但这不是我需要的,我需要的是显示用户的姓名,并将其显示为链接到他们的个人资料,就像它在草图中显示的那样。当我查看木材文档时,那里似乎没有太多关于 ACF 的信息。如果它只是 php,我很容易找到解决方法,但我完全迷失了,因为它是木材。

有人帮我解决了以下问题:

查看有关 Timber / ACF 集成的文档:

https://timber.github.io/docs/guides/acf-cookbook/

此外,Timber/Post 有一个名为 author 的方法:

https://timber.github.io/docs/reference/timber-post/#author

参见下面的例子,它有一个属性 post.author.ID

  <a href="{{post.author.link}}">{{post.author.name}}</a>

我总是觉得有用的是{{ dump( post ) }},只要记住启用 WP_DEBUG 即可。

您是否通过自定义字段分配作者?然后,您可以尝试使用{{ post.get_field(" field_name") }}并将其分配给诸如{% set author = post.get_field(" field_name") %} then {{ dump() }}输出之类的变量,以查看正在返回的数据。

您需要转储变量,因此它应该是 {% set author = post.get_field("auto") %} 然后 {{ dump(author) }},使用 WP_DEBUG 在您的 wp-config 中启用

否则,请确保将 Post\Timber 对象添加到 $context 而不是 WP_Post。

我做了所说的,这是我在输出中收到的结果:

then array(1) { [0]=> array(11) { ["ID"]=> int(1) ["user_firstname"]=> string(7) "John" ["user_lastname"]=> string(5) "Doe" ["nickname"]=> string(4) "john" ["user_nicename"]=> string(13) "johndoe" ["display_name"]=> string(13) "John Doe" ["user_email"]=> string(23) "contact@site.com" ["user_url"]=> string(23) "https://siteDOTcom" ["user_registered"]=> string(19) "2019-03-12 03:53:10" ["user_description"]=> string(0) "" ["user_avatar"]=> string(472) "John Doe" } }

但现在我不知道如何将它与 Timber 一起使用,因为 Timber 不接受 PHP。

这是我试图用来解决此问题的另一种方法,但没有成功: https ://stackoverflow.com/a/57478272/11457250

标签: phpwordpressadvanced-custom-fieldstimbergantry-framework

解决方案


Timber 使用称为 twig 的模板语言,使用双花括号输出值。一旦你设置了变量作者,你就可以访问它们的数组值,我将密钥固定到变量上,即。{{ author.user_firstname }}


推荐阅读