• Resolved fabianborg

    (@fabianborg)


    Hi, I am trying to echo the image dimensions of a custom logo in my header.php. This is my code so far

    functions.php

    function mytheme_customize_register( $wp_customize ) {
    $wp_customize->add_section( 'custom_logo_section' , array(
    'title'         => __( 'Logo', 'custom' ),
    'priority'      => 30,
    'description'   => 'Upload a logo to replace the default site name and description in the header',
    ));
    $wp_customize->add_setting( 'custom_logo' );
    $wp_customize->add_control( new WP_Customize_Image_Control(
    $wp_customize, 'custom_logo', array(
    'label'     => __( 'Logo', 'custom' ),
    'section'   => 'custom_logo_section',
    'settings'  => 'custom_logo',
    )
    ));
    }

    header.php

    <?php if( get_theme_mod( 'custom_logo') ) : ?>
    <a href='<?php echo esc_url( home_url( '/' ) ); ?>' title='<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?> 'rel='home' class="navbar-brand"><img src='<?php echo esc_url( get_theme_mod( 'custom_logo' ) ); ?> 'width='<?php echo $width; ?>' 'height='<?php echo $height; ?>' 'alt='<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>'></a></div>

    The width and height integers are not displaying the image width and height dimensions. How can I solve this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @fabianborg,

    Did you define $width and $height variables anywhere in header.php ?
    Can you also please post the entire code(like the initialization of those two variables).

    Thread Starter fabianborg

    (@fabianborg)

    Hi @bravokey, no I did not define the variables in the header.php and neither in the functions.php were the custom_logo is created as I thought that the image properties would be transposed automatically.

    As far as the code goes, I have posted all the relevant code.

    What would be your suggestion, please?

    As far as I know those variables are not defined and they are available when you are trying to echo.

    You can try the following code which uses PHP getimagesize function.

    <?php if ( get_theme_mod( 'custom_logo' ) ) :
        $logo = get_theme_mod( 'custom_logo' );
        $logo_size = getimagesize($logo);
        $width = $logo_size[0];
        $height = $logo_size[1];
    ?>
    <a href='<?php echo esc_url( home_url( '/' ) ); ?>' title='<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?> 'rel='home' class="navbar-brand"><img src='<?php echo esc_url( get_theme_mod( 'custom_logo' ) ); ?> 'width='<?php echo $width; ?>' 'height='<?php echo $height; ?>' 'alt='<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>'></a></div>

    Taken from WPSE

    Thread Starter fabianborg

    (@fabianborg)

    @bravokey

    The solution provided worked out of the box. Thanks for that, I was overcomplicating things with get_image_tag() to make use of the string $size as myslef being new to wordpress I was not aware if wordpress supported getimagesize.

    Once again thanks for the help 😉

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Echoing image dimensions’ is closed to new replies.