Php 在木材/细枝主题中使用WordPress自定义标题图像

Php 在木材/细枝主题中使用WordPress自定义标题图像,php,wordpress,twig,wordpress-theming,timber,Php,Wordpress,Twig,Wordpress Theming,Timber,WordPress/木材/树枝主题新手。我想把主题自定义标题图像到一个木材为基础的细枝主题。我知道您需要绑定到主题定制器API,并使用此代码{function('get_theme_mod','name')},但我不知道如何调整它以处理自定义标题图像 有什么建议或提示吗?我已经找到了功能代码。我已经使用{function('get_theme_mod','header_image')}}访问了图像源代码,但现在我想知道如何使用木材/树枝访问为自定义标题图像定义的alt文本。您可以使用以下方法从c

WordPress/木材/树枝主题新手。我想把主题自定义标题图像到一个木材为基础的细枝主题。我知道您需要绑定到主题定制器API,并使用此代码
{function('get_theme_mod','name')}
,但我不知道如何调整它以处理自定义标题图像


有什么建议或提示吗?

我已经找到了功能代码。我已经使用
{function('get_theme_mod','header_image')}}
访问了图像源代码,但现在我想知道如何使用木材/树枝访问为自定义标题图像定义的alt文本。

您可以使用以下方法从customizer获取图像:

{{theme.theme\u mod('header\u image')

如果您想要获得
alt
属性,那么您可以创建一个过滤器来获取 图像URL的alt属性

首先,在functions.php中创建一个函数:

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 }}