0

I have added a few sections to the customizer but am having trouble when trying to call the image I have defined. The back end works great but when I call it using get_theme_mod it echos a two digit number, like 23. I have several sections in the theme customizer (non-images) and don't have any issues.

In functions.php (simplified to just this one example):

function theme_customize_register( $wp_customize ) {
    $wp_customize->add_section( 'theme_about' , array(
    'title'      => __( 'About', 'theme' ),
    'description' => 'Add phone, email, and social buttons'
    ) );

    $wp_customize->add_setting( 'about_facebook_logo' );
    $wp_customize->add_control( new WP_Customize_Cropped_Image_Control( $wp_customize, 'about_facebook_logo' , array(
    'label' => __('Facebook Image', 'theme'),
    'section' => 'theme_about',
    'settings' => 'about_facebook_logo',
    'flex_width'  => true,
    'flex_height' => true,
    'width'       => 46,
    'height'      => 46,
    ) ));
}
add_action( 'customize_register', 'theme_customize_register' );

In my footer.php:

<?php echo get_theme_mod( 'about_facebook_logo' ); ?>

This outputs the number 23.

ANSWER

Thanks to Rarst's thoughts below, I was able to come up with a solution.

<?php $fbID = get_theme_mod( 'about_facebook_logo' );
      echo wp_get_attachment_url( $fbID ); ?>
1

I am not very familiar with Customizer part of code, but my immediate guess would that number is attachment ID for the uploaded image.

IDs are most typical and reliable way to store connection to particular media item within WP installation. It is trivial to get from ID to other representations, such as URLs to different image sizes. However it is quite challenging to go in reverse direction, such as determine ID (if any) for arbitraty image URL.

You can check if number matches up with ID in admin (typically exposed in URL) and use API functions on it (such as wp_get_attachment_image_src() and others).

  • That was my thought too but according to several sources I've found online my syntax should produce the result I'm looking for... – Heather Sep 19 '16 at 20:27
  • That got me thinking and I was able to grab the URL using: $fbID = get_theme_mod( 'about_facebook_logo' ); echo wp_get_attachment_url( $fbID ); – Heather Sep 19 '16 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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