如何使用 get_theme_mod 获取图像的替代文本?

lif*_*win 4 php wordpress

我想创建通过Wordpress 自定义功能加载徽标的可能性。当我使用插入徽标时get_theme_mod()- 它只是图像的 url,但我也想获取此图像的替代文本。

我的代码示例:

函数.php

function welbit_theme_customizer( $wp_customize ) {

    $wp_customize->add_section( 'welbit_logo_section' , array(
    'title'       => __( '???????', 'themeslug' ),
    'priority'    => 30,
    'description' => '????????? ????? ???????????.',
    ) );

    $wp_customize->add_setting( 'welbit_logo' );

    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'welbit_logo', array(
        'label'    => __( 'Logo', 'welbit' ),
        'section'  => 'welbit_logo_section',
        'settings' => 'welbit_logo',
    ) ) );
}

add_action( 'customize_register', 'welbit_theme_customizer' );
Run Code Online (Sandbox Code Playgroud)

头文件

<div class="w-header__logo">
    <a href="<?php echo get_home_url(); ?>">
       <img src="<?php print_r(get_theme_mod('welbit_logo'));?>" alt="<!-- How can I get this? ->" class="logo">
    </a>
</div>
Run Code Online (Sandbox Code Playgroud)

Dev*_*tle 5

好的,我找到了我一直在寻找几天的网络上没有人的答案。

这是我如何做到的。希望这可以帮助那里的人

// This is getting the image / url
$feature1 = get_theme_mod('feature_image_1');

// This is getting the post id
$feature1_id = attachment_url_to_postid($feature1);

// This is getting the alt text from the image that is set in the media area
$image1_alt = get_post_meta( $feature1_id, '_wp_attachment_image_alt', true );
Run Code Online (Sandbox Code Playgroud)

标记

<a href="<?php echo $feature1_url; ?>"><img class="img-responsive center-block" src="<?php echo $feature1; ?>" alt="<?php echo $image1_alt; ?>"></a>
Run Code Online (Sandbox Code Playgroud)