3

So inside this theme I'm making for a client there is a section inside the customizer that allows them to change a header on each individual page, my question is how do I fetch the alt tag of that Image?

The Images are uploaded through the Media Library so the alt text has been set after the Image was uploaded.

Below is what I have tried to fetch the Image alt tag

$img_id = get_post_thumbnail_id(get_the_ID());
$alt_text = get_post_meta($img_id , '_wp_attachment_image_alt', true);

<img src="<?php echo get_theme_mod('about-header', get_stylesheet_directory_uri() . '/assests/imgs/placeholder.png'); ?>" alt="<?php echo $alt_text; ?>">

For some reason it just doesn't seem to get the Images alt tag and I don't understand why

Code for the Customizer (just in case I have to do something inside there)

$wp_customize->add_section('page_header', array(
    'title'         => __('Page Headers', 'bissell-theme'),
    'priority'      => 30,
    'description'   => __('Below are the options to change your header images for all your pages. Just simply click \'Select Image\' and choose a Image from the Media Libary or Upload one your self'),
));

$wp_customize->add_setting('about-header');

$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'about_header_control', array(
    'label'         => __('About Us - Header'),
    'section'       => 'page_header',
    'settings'      => 'about-header',
)));
3
  • you have a typo $alt_text;) remove the bracket and recheck...
    – majick
    Commented Mar 23, 2016 at 12:29
  • @majick Oh xD That actually isn't in my code I must of just put it on here by mistake my bad :D
    – Stephen
    Commented Mar 23, 2016 at 13:43
  • ah k no worries, the 500 suggests a syntax error, maybe check everything is properly wrapped in php tags in the template? oh wait, i see it...
    – majick
    Commented Mar 23, 2016 at 14:30

2 Answers 2

2

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

I believe the problem may be with not providing arguments to the customizer add_setting call, especially since the type needs to be set to theme_mod...

$wp_customize->add_setting('about-header',array(
    'type' => 'theme_mod', 'capability' => 'edit_theme_options', 'default' => '', 'transport' => 'refresh'
) );
3
  • I don't think that is the problem, cause to my knowledge if you don't specify what the setting are then the defaults setting are used and in this case what is above is the default settings. Thanks for the reply though :)
    – Stephen
    Commented Mar 23, 2016 at 14:53
  • ah yeah you're right, i was thinking option was default type. whoops. :)
    – majick
    Commented Mar 24, 2016 at 2:27
  • Aha no problem xD
    – Stephen
    Commented Mar 24, 2016 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.