How to get alternate text of image using get_theme_mod?

I want to create the possibility to load a logo via WordPress Customize feature. When I insert logo using get_theme_mod() – it is only url of image, but I also want get the alternate text of this image.

My code example:

Read More

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>

Related posts

1 comment

  1. Ok I found the answer that no one has on the net I been looking for days now.

    Here is how I was able to do it. Hope this helps someone out there

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

    Markup

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

Comments are closed.