首页 > 解决方案 > 无法使用 WP_Customize_Image_Cropped_Control 从 get_theme_mod 获取价值

问题描述

我无法从 WP_Customize_Cropped_Image_Control 输出值。

我不明白我做错了什么,我的图像在定制器中挑选和保存很好,但无法输出这个图像。这是我的定制器代码:

    $wp_customize->add_setting('mobile_logo', array(
        'transport' => 'postMessage',
        'sanitize_callback' => 'absint'
    ));
    $wp_customize->add_control( new WP_Customize_Cropped_Image_Control( $wp_customize, 'mobile_logo', array(
        'label' => esc_html__( 'Mobile Logo', 'my_theme' ),
        'height' => 80,
        'width' => 120,
        'flex-height' => true,
        'flex-width' => true,
        'settings' => 'mobile_logo',
        'section' => 'title_tagline',
    )));

我的输出代码:

<?php
    $mobile_logo = get_theme_mod( '_mobile_logo' );
?>
<img src="<?php echo esc_url($mobile_logo); ?>">

我尝试了 var_dump $mobile_logo,我得到的只是字符串(0)“”。

你能帮我吗?谢谢。

标签: wordpresswordpress-theming

解决方案


我找到了答案,我应该使用 wp_get_attachment_image_url()。所以答案是:

<?php
    $mobile_logo = get_theme_mod( 'mobile_logo' );
?>
<img src="<?php echo esc_url(wp_get_attachment_image_url($mobile_logo)); ?>">

推荐阅读