gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:57:37 28 4
gpt4 key购买 nike

我想创建通过 Wordpress 自定义 功能加载 Logo 的可能性。当我使用 get_theme_mod() 插入 Logo 时 - 它只是图像的 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' );

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>

最佳答案

好的,我找到了网上没有人找到的答案,我已经找了好几天了。

这是我如何做到的。希望这可以帮助那里的人

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

关于php - 如何使用 get_theme_mod 获取图像的替代文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36869499/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com