Pregunta

I try to get cropped image src on my subdomain multisite, but not working. My code here:

// avatar uploader
$wp_customize->add_setting( 'bwpy_logo2', array(
    'sanitize_callback' => 'absint', 
    'default'           => ''
) );
$wp_customize->add_control( new WP_Customize_Cropped_Image_Control( $wp_customize, 'bwpy_logo2', array(
    'label'       => __( 'Site Avatar', 'bwpy' ),
    'section'     => 'title_tagline',
    'settings'    => 'bwpy_logo2',
    'flex-width'  => true,
    'width'       => 300,
    'flex-height' => true,
    'height'      => 300,
    'priority'    => 21
) ) );

And this is, how I try to get this in the current theme header:

<a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
    <?php if ( get_theme_mod( 'bwpy_logo2' ) ) : ?>
    <img src="<?php echo wp_get_attachment_image_src( absint( get_theme_mod( 'bwpy_logo2' ) ) ); ?>" alt="<?php echo get_bloginfo('name'); ?>" class="header-avatar">
    <?php else : ?>
    <img src="default-site-icon.png" alt="<?php echo get_bloginfo('name'); ?>" class="header-avatar">
    <?php endif; ?>
</a>

Why not working this?

the output scr this: Array so: <img src="Array" alt="Blog Title" class="header-avatar">

¿Fue útil?

Solución

wp_get_attachment_image_src() function returns an array that's why your code isn't working. Try the following code.

<a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
    <?php
    if ( get_theme_mod( 'bwpy_logo2' ) ) :
        $img_data = wp_get_attachment_image_src( absint( get_theme_mod( 'bwpy_logo2' ) ) );
        $img_url = isset( $img_data[0] ) ? $img_data[0] : '';
        // Or you can try this
        // $img_url = wp_get_attachment_image_url( absint( get_theme_mod( 'bwpy_logo2' ) ) );
        ?>
        <img src="<?php echo esc_url( $img_url ); ?>" alt="<?php echo get_bloginfo('name'); ?>" class="header-avatar">
    <?php else : ?>
        <img src="default-site-icon.png" alt="<?php echo get_bloginfo('name'); ?>" class="header-avatar">
    <?php endif; ?>
</a>
Licenciado bajo: CC-BY-SA con atribución
scroll top