Skip to content

Commit

Permalink
fix: handle attachement fetch failure (#2301)
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek authored May 23, 2024
1 parent 1a2cb72 commit 6bdffe0
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions newspack-theme/inc/template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,25 @@ function newspack_featured_image_position() {
return '';
}

// Get thumbnail
$thumbnail_info = wp_get_attachment_metadata( get_post_thumbnail_id() );
$img_width = $thumbnail_info['width'];
$position = is_single() ? get_theme_mod( 'featured_image_default', 'large' ) : get_theme_mod( 'page_featured_image_default', 'small' );

// Get per-post image position setting.
$image_pos = get_post_meta( get_the_ID(), 'newspack_featured_image_position', true );

// Set a position value to return.
$position = '';

// First, check for a per-post image setting.
if ( '' !== $image_pos ) {
$position = $image_pos;
// If this post doesn't have a setting, fall back to the default.
} else {
if ( is_single() ) {
// Set default if a post:
$position = get_theme_mod( 'featured_image_default', 'large' );
} elseif ( is_page() ) {
// Set default if a page:
$position = get_theme_mod( 'page_featured_image_default', 'small' );
}
}

if ( ( 'large' === $position && 1200 > $img_width ) || ! in_array( get_post_type(), newspack_get_featured_image_post_types() ) ) {
// Get thumbnail
$thumbnail_info = wp_get_attachment_metadata( get_post_thumbnail_id() );
if ( $thumbnail_info === false ) {
return $position;
}

$image_wide_width = 1200;
if ( (
'large' === $position && $image_wide_width > $thumbnail_info['width'] )
|| ! in_array( get_post_type(), newspack_get_featured_image_post_types() )
) {
$position = 'small';
}

Expand Down

0 comments on commit 6bdffe0

Please sign in to comment.