Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 木材:从另一页访问高级自定义字段_Php_Wordpress_Twig_Timber - Fatal编程技术网

Php 木材:从另一页访问高级自定义字段

Php 木材:从另一页访问高级自定义字段,php,wordpress,twig,timber,Php,Wordpress,Twig,Timber,我正在尝试使用木材(细枝)从另一个页面访问要显示在另一个页面上的ACF数据 ACF名称是“关于”页面中的英雄(id=7) page home.php: 在上面的示例中,我看到您从about页面获取字段数据,但您没有将其添加到上下文中。您的模板不显示该数据,因为您没有将其交给模板 首先设置上下文: $context = Timber::get_context(); 然后获得应显示的当前post数据: $post = new TimberPost(); 现在您确实加载了$post,但它还不在您的

我正在尝试使用木材(细枝)从另一个页面访问要显示在另一个页面上的ACF数据

ACF名称是“关于”页面中的英雄(id=7)

page home.php:


在上面的示例中,我看到您从about页面获取字段数据,但您没有将其添加到上下文中。您的模板不显示该数据,因为您没有将其交给模板

首先设置上下文:

$context = Timber::get_context();
然后获得应显示的当前post数据:

$post = new TimberPost();
现在您确实加载了
$post
,但它还不在您的上下文中。您必须将要在页面上显示的数据放入
$context
数组中。然后通过
Timber::render('template.twig',$context)
渲染它。您的细枝模板将只包含
$context
中存在的数据(完整点:您也可以使用细枝模板中的函数获取数据,但这是另一个主题)

要添加从“关于”页面加载的数据,必须执行以下操作:

$about_page_id = 7;
$about = new TimberPost( $about_page_id );
$context['about'] = $about;
看到行
$about->acf=get\u field\u objects($about->ID)
不再存在了吗?您不需要它,因为Timber会自动将ACF字段加载到post数据中。您的字段现在可以通过小枝模板中的
{{about.the_unstrung_hero}}
访问

回到你想要实现的目标:

我会这样解决它。 就像在你问题的评论中提到的End一样,我也会使用
get_field()
函数的第二个参数来从一个post-by-post-ID获取字段数据

如果您只想显示一个ACF字段的值,那么实际上不需要加载about页面的整个帖子

page home.php

$context=Timber::get_context();
$post=新木桩();
$context['post']=$post;
//将ACF字段数据添加到上下文
$about_page_id=7;
$context['the_unstrung_hero']=get_字段('the_unstrung_hero',$about_page_id);
木材::渲染(数组('page-'.$post->post_name'..twig','page.twig'),$context);
然后在page home.twig中的中,您可以访问post中的字段数据

<p>{{ the_unstrung_hero }}</p>
{{{英雄}


您是否尝试过使用acf字段id而不是名称“英雄”?如何获取acf字段“id”?THNX在添加/编辑ACF字段(字段组)时,单击“屏幕选项”,您将看到一个复选框“字段键”。检查后,您将能够在字段的左侧看到“字段键”(或我使用的id)。我尝试了字段键-因为您说的是“字段id”,所以我感到困惑。您能尝试一下这个
字段('the_unstrung_hero',7)吗
$about_page_id = 7;
$about = new TimberPost( $about_page_id );
$context['about'] = $about;
<p>{{ the_unstrung_hero }}</p>