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

WordPress / Timber / Twig主题新手在这里。我想将主题自定义标题图像引入基于Timber的Twig主题中。我知道您需要绑定到主题定制器API并获得此代码{{ function('get_theme_mod','name') }},但是我不知道如何使它适应自定义标题图像。

有什么建议或提示吗?

nengdong213 回答:在木材/嫩枝主题中使用WordPress自定义标题图像

我已经弄清楚了功能代码。我已经使用{{ function('get_theme_mod','header_image') }}访问了图像源,但是现在我想知道如何使用Timber / Twig访问为自定义标题图像定义的替代文本。

,

您可以使用以下方法从定制器获取图像:

{{ theme.theme_mod('header_image')

如果您想获取alt属性,则可以创建一个过滤器,以获取 图片网址的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 }}
本文链接:https://www.f2er.com/3098878.html

大家都在问