$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to get image ID based on get_theme_mod image URL?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to get image ID based on get_theme_mod image URL?

matteradmin8PV0评论

I'm trying to use wp_get_attachment_image function to insert image. For that I need use image ID. So now I need to know how to get this ID by image URL taken from get_theme_mod from WP Customization API.

So:

$image_src = get_theme_mod('header_logo', '');
$image_id = HOW TO GET ID FROM $image_src ??
echo wp_get_attachment_image( $image_id, 'logo' );

I found this solution so I was trying:

// retrieves the attachment ID from the file URL
function pippin_get_image_id($image_url) {
    global $wpdb;
    $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); 
        return $attachment[0]; 
}

// set the image url
$image_url = get_theme_mod('header_logo', '');

// store the image ID in a var
$image_id = pippin_get_image_id($image_url);

// print the id
echo $image_id;

// final image
echo wp_get_attachment_image( $image_id, 'logo' );

but still nothing. Any ideas?

I'm trying to use wp_get_attachment_image function to insert image. For that I need use image ID. So now I need to know how to get this ID by image URL taken from get_theme_mod from WP Customization API.

So:

$image_src = get_theme_mod('header_logo', '');
$image_id = HOW TO GET ID FROM $image_src ??
echo wp_get_attachment_image( $image_id, 'logo' );

I found this solution https://stackoverflow/questions/25671108/get-attachment-id-by-file-path-in-wordpress/31743463 so I was trying:

// retrieves the attachment ID from the file URL
function pippin_get_image_id($image_url) {
    global $wpdb;
    $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); 
        return $attachment[0]; 
}

// set the image url
$image_url = get_theme_mod('header_logo', '');

// store the image ID in a var
$image_id = pippin_get_image_id($image_url);

// print the id
echo $image_id;

// final image
echo wp_get_attachment_image( $image_id, 'logo' );

but still nothing. Any ideas?

Share Improve this question asked Feb 19, 2019 at 22:34 DamianDamian 971 gold badge1 silver badge11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

WordPress has a built-in function for this: attachment_url_to_postid().

More info from a Stack Overflow question: Get Attachment ID by File Path in WordPress.

Post a comment

comment list (0)

  1. No comments so far