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

时间:2016-04-26 15:33:55

标签: php wordpress

我想通过 Wordpress Customize 功能创建加载徽标的可能性。当我使用get_theme_mod()插入徽标时 - 它只是图片的网址,但我也希望获得此图片的替代文字。

我的代码示例:

的functions.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' );

header.php

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

1 个答案:

答案 0 :(得分:1)

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

以下是我能够做到的。希望这有助于那里的人

// 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 );

标记

<a href="<?php echo $feature1_url; ?>"><img class="img-responsive center-block" src="<?php echo $feature1; ?>" alt="<?php echo $image1_alt; ?>"></a>