Make WordPress Core

Opened 8 years ago

Last modified 11 days ago

#37972 new defect (bug)

Image size (height/width) arguments are not used in output for custom_logo

Reported by: binarykitten's profile BinaryKitten Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.6
Component: Themes Keywords: has-patch
Focuses: Cc:

Description

when setting up the custom logo in theme support, it allows for height/width to be set.

When outputting, the default and only option for the output (via the_custom_logo() ) is "full" (as set in the get_custom_logo function call)

This is carry over from commit
[37135] for #36327

Change History (3)

This ticket was mentioned in Slack in #themereview by joyously. View the logs.


7 years ago

This ticket was mentioned in PR #1423 on WordPress/wordpress-develop by paulschreiber.


3 years ago
#2

  • Keywords has-patch added

Fetch the height and width from the custom logo settings. If present, pass them in as a size parameter to wp_get_attachment_image().

Trac ticket: https://core.trac.wordpress.org/ticket/37972

@hemak commented on PR #1423:


5 weeks ago
#3

To fetch the height and width from the custom logo settings in WordPress and pass them as a size parameter to wp_get_attachment_image(), you can use the following approach:

  1. Get the Custom Logo ID: Use get_theme_mod( 'custom_logo' ) to retrieve the custom logo ID.
  2. Get the Image Metadata: Use wp_get_attachment_metadata() to fetch the logo’s width and height.
  3. Pass Dimensions to wp_get_attachment_image(): Once you have the dimensions, pass them in the size parameter when calling wp_get_attachment_image().

Here’s an example code snippet:

// Get the custom logo ID
$custom_logo_id = get_theme_mod( 'custom_logo' );

// Check if a custom logo is set
if ( $custom_logo_id ) {
    // Get the logo metadata
    $logo_metadata = wp_get_attachment_metadata( $custom_logo_id );

    // Extract width and height if available
    $width = $logo_metadata['width'] ?? '';
    $height = $logo_metadata['height'] ?? '';

    // Pass width and height to wp_get_attachment_image as an array
    echo wp_get_attachment_image( $custom_logo_id, [ $width, $height ] );
}

<sub><sup style="font-size: 0.1em;">.</sup></sub>

This code checks if a custom logo exists, retrieves its dimensions, and then passes those dimensions as an array to wp_get_attachment_image(). This approach ensures the custom logo appears at its correct size.

Note: See TracTickets for help on using tickets.