使用 Timber 获取 ACF OEmbed URL 而不返回 iFrame

问题描述 投票:0回答:2

我有一个半复杂的灵活内容组件,用于访问包含 oembed ACF 字段的 postObject。

换句话说,灵活内容 -> PostObject -> OEmbed

但我想提取原始 OEmbed 链接以放入其自己的“A 标签”中,以便将人们发送到视频源。

我已经研究过这些问题:
https://github.com/timber/timber/issues/1211
https://github.com/timber/timber/issues/1423

类似,但似乎不适用于我的情况。

有没有办法
A) 拉出 Twig 模板中的链接或
B) 扩展 Timber 以仅返回 PostObject 中某处的链接?

wordpress advanced-custom-fields timber
2个回答
0
投票

我建议提取 php 中的链接,然后将其添加到上下文中:

// Load value.
$iframe = get_field('oembed');

// Use preg_match to find iframe src.
preg_match('/src="(.+?)"/', $iframe, $matches);
$src = $matches[1];

// Add to timber/twig context
$context['iframe_src'] = $src

0
投票

您还可以通过将

false
作为第三个参数传递给
get_field
函数来获取不带 iframe 格式的 oEmbed。

// Load oEmbed URL
$iframeUrl = get_field('oembed', false, false);

更多信息请参见ACF 函数文档页面

© www.soinside.com 2019 - 2024. All rights reserved.