从已通过定制器上载的图像获取img alt标记

时间:2016-03-23 作者:Stephen

因此,在我为客户机制作的这个主题中,自定义程序中有一个部分,允许他们更改每个页面上的标题,我的问题是如何获取该图像的alt标记?

图像通过媒体库上载,因此在上载图像后设置了alt文本。

下面是我尝试获取图像alt标记的内容

$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; ?>">
出于某种原因,它似乎没有得到图片alt标签,我不明白为什么

自定义程序的代码(以防我必须在其中执行某些操作)

$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\',
)));

2 个回复
SO网友:DevTurtle

好吧,我找到了答案,我已经在网上找了好几天了。

下面是我如何做到这一点的。希望这对其他人有帮助

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

SO网友:majick

我认为问题可能在于没有向定制者提供参数add_setting 调用,尤其是因为需要将类型设置为theme\\u mod。。。

$wp_customize->add_setting(\'about-header\',array(
    \'type\' => \'theme_mod\', \'capability\' => \'edit_theme_options\', \'default\' => \'\', \'transport\' => \'refresh\'
) );

相关推荐

Images with overlay

我有一些图片在一个容器中,我想添加一个覆盖和图标。这不是现成的,但我找到了一些有用的代码:HTML:<div class=\"main\"> <span class=\"featured\"><img src=\"http://joshrodg.com/IMG_001-258x258.jpg\" title=\"\" alt=\"\"></span> </div> CSS:.featured {