Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function find_image_post_id($url) {
- global $wpdb;
- $postid = $wpdb->get_var($wpdb->prepare("SELECT DISTINCT ID FROM $wpdb->posts WHERE guid='$url'"));
- if ($postid) {
- return $postid;
- }
- return false;
- }
- if ( ! function_exists( 'get_attachment_id' ) ) {
- /**
- * Get the Attachment ID for a given image URL.
- *
- * @link http://wordpress.stackexchange.com/a/7094
- *
- * @param string $url
- *
- * @return boolean|integer
- */
- function get_attachment_id( $url ) {
- $dir = wp_upload_dir();
- // baseurl never has a trailing slash
- if ( false === strpos( $url, $dir['baseurl'] . '/' ) ) {
- // URL points to a place outside of upload directory
- return false;
- }
- $file = basename( $url );
- $query = array(
- 'post_type' => 'attachment',
- 'fields' => 'ids',
- 'meta_query' => array(
- array(
- 'key' => '_wp_attached_file',
- 'value' => $file,
- 'compare' => 'LIKE',
- ),
- )
- );
- // query attachments
- $ids = get_posts( $query );
- if ( ! empty( $ids ) ) {
- foreach ( $ids as $id ) {
- // first entry of returned array is the URL
- if ( $url === array_shift( wp_get_attachment_image_src( $id, 'full' ) ) )
- return $id;
- }
- }
- $query['meta_query'][0]['key'] = '_wp_attachment_metadata';
- // query attachments again
- $ids = get_posts( $query );
- if ( empty( $ids) )
- return false;
- foreach ( $ids as $id ) {
- $meta = wp_get_attachment_metadata( $id );
- foreach ( $meta['sizes'] as $size => $values ) {
- if ( $values['file'] === $file && $url === array_shift( wp_get_attachment_image_src( $id, $size ) ) )
- return $id;
- }
- }
- return false;
- }
- }
- $attachment_id = attachment_url_to_postid( $image_url );
- echo $attachment_id;
- function get_attachment_id( $url, $ignore_path = false ) {
- if ( ! $ignore_path ) {
- $dir = wp_upload_dir();
- $dir = trailingslashit($dir['baseurl']);
- if( false === strpos( $url, $dir ) )
- return false;
- }
- $file = basename($url);
- $query = array(
- 'post_type' => 'attachment',
- 'fields' => 'ids',
- 'meta_query' => array(
- array(
- 'key' => '_wp_attached_file',
- 'value' => $file,
- 'compare' => 'LIKE',
- )
- )
- );
- $ids = get_posts( $query );
- foreach( $ids as $id ) {
- $match = array_shift( wp_get_attachment_image_src($id, 'full') );
- if( $url == $match || ( $ignore_path && strstr( $match, $file ) ) )
- return $id;
- }
- $query['meta_query'][0]['key'] = '_wp_attachment_metadata';
- $ids = get_posts( $query );
- foreach( $ids as $id ) {
- $meta = wp_get_attachment_metadata($id);
- foreach( $meta['sizes'] as $size => $values ) {
- if( $values['file'] == $file && ( $ignore_path || $url == array_shift( wp_get_attachment_image_src($id, $size) ) ) )
- return $id;
- }
- }
- return false;
- }
- // This is getting the image / url
- $feature1 = get_theme_mod('feature_image_1');
- // This is getting the post id
- $feature1_id = attachment_url_to_postid($feature1);
- // This is getting the alt text from the image that is set in the media area
- $image1_alt = get_post_meta( $feature1_id, '_wp_attachment_image_alt', true );
- <a href="<?php echo $feature1_url; ?>"><img class="img-responsive center-block" src="<?php echo $feature1; ?>" alt="<?php echo $image1_alt; ?>"></a>
- $image_url = get_field('main_image'); // in case of custom field usage
- $image_id = attachment_url_to_postid($image_url);
- // retrieve the thumbnail size of our image
- $image_thumb = wp_get_attachment_image_src($image_id, 'thumbnail');
Add Comment
Please, Sign In to add comment