[英]Use WordPress custom header image in Timber/Twig theme
WordPress/Timber/Twig 主题新手在这里。 我想将主题自定义 header 图像带入基于木材的 Twig 主题。 我知道您需要绑定到主题定制器 API 并拥有此代码{{ function('get_theme_mod', 'name') }}
,但我不知道如何使其适用于自定义 header 图像。
有什么建议或提示吗?
我已经弄清楚了 function 代码。 我已经使用{{ function('get_theme_mod', 'header_image') }}
访问了图像源,但现在我想知道如何使用 Timber/Twig 访问为自定义 header 图像定义的 alt 文本。
您可以使用以下方法从定制器获取图像:
{{ theme.theme_mod('header_image')
如果要获取alt
属性,则可以创建一个过滤器,该过滤器可以获取图像 URL 的 alt 属性。
首先,在functions.php中创建一个function:
public function altText( $url ) {
$feature1_id = attachment_url_to_postid( $url );
$image1_alt = get_post_meta( $feature1_id, '_wp_attachment_image_alt', true );
return $image1_alt;
}
然后将其添加为过滤器:
$twig->addFilter( new Twig\TwigFilter( 'altText', array( $this, 'altText' ) ) );
然后在模板中使用它:
{{ theme.theme_mod('header_image') | altText }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.