我想创建通过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' );
标头.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>
好的,
我找到了我几天来一直在寻找的网络上没有人的答案。
这是我能够做到的。希望这对那里的人有所帮助
// 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>