public function getOutput($item, $fieldId, $field) { if ($field['type'] == 'text') { return "<span class='custom-item-{$item->ID} custom-item-{$fieldId} custom-item-text'>" . $item->{$fieldId} . '</span>'; } elseif ($field['type'] == 'textarea') { return "<p class='custom-item-{$item->ID} custom-item-{$fieldId} custom-item-textarea'>" . $this->_parseString($item->{$fieldId}) . '</p>'; } elseif ($field['type'] == 'upload') { $image_id = yit_get_attachment_id($item->{$fieldId}); $thumb_size = apply_filters('yit_nav_thumb_size', 'thumb_portfolio_4cols'); $image = yit_image("id=" . $image_id . "&size=" . $thumb_size . "&output=array"); if (count($image) != 3) { return; } list($thumbnail_url, $thumbnail_width, $thumbnail_height) = $image; return "<a class='custom-item-{$item->ID} custom-item-{$fieldId} custom-item-image' href='" . esc_attr($item->url) . "'><img src='" . yit_strip_protocol($thumbnail_url) . "' alt='" . apply_filters('the_title', $item->title, $item->ID) . "' /></a>"; } }
/** * Return the ID of an attachment. * * @param string $url * @return int * @since 1.0.0 */ function yit_get_attachment_id($url) { $dir = wp_upload_dir(); $dir = trailingslashit(YIT_WPCONTENT_URL); if (false === strpos($url, $dir)) { return false; } $file = basename($url); $query = array('post_type' => 'attachment', 'fields' => 'ids', 'meta_query' => array(array('value' => $file, 'compare' => 'LIKE'))); $query['meta_query'][0]['key'] = '_wp_attached_file'; $ids = get_posts($query); foreach ($ids as $id) { $attachment_image = wp_get_attachment_image_src($id, 'full'); $db_url = yit_strip_protocol(array_shift($attachment_image)); $url = yit_strip_protocol($url); if ($url == $db_url) { return $id; } } $query['meta_query'][0]['key'] = '_wp_attachment_metadata'; $ids = get_posts($query); foreach ($ids as $id) { $meta = wp_get_attachment_metadata($id); if (!isset($meta['sizes'])) { continue; } foreach ((array) $meta['sizes'] as $size => $values) { $attachment_image = wp_get_attachment_image_src($id, $size); if ($values['file'] == $file && $url == array_shift($attachment_image)) { return $id; } } } return false; }
public function getOutput( $item, $fieldId, $field ) { if( $field['type'] == 'text' ) { return "<span class='custom-item-{$item->ID} custom-item-{$fieldId} custom-item-text'>" . $item->{$fieldId} . '</span>'; } elseif( $field['type'] == 'textarea' ) { return "<p class='custom-item-{$item->ID} custom-item-{$fieldId} custom-item-textarea'>" . $this->_parseString($item->{$fieldId}) . '</p>'; } elseif( $field['type'] == 'upload' ) { $image_id = yit_get_attachment_id( $item->{$fieldId} ); $thumb_size = apply_filters( 'yit_nav_thumb_size', 'thumb_portfolio_4cols' ); $image = yit_image( "id=" . $image_id . "&size=" . $thumb_size . "&output=array" ); if ( count( $image ) != 3 ) return; list( $thumbnail_url, $thumbnail_width, $thumbnail_height ) = $image; $img = '<img src="' . yit_strip_protocol( $thumbnail_url ) . '" alt="' . apply_filters( 'the_title', $item->title, $item->ID ) . '" />'; $classes = array( 'custom-item-' . $item->ID, 'custom-item-' . $fieldId, 'custom-item-image' ); $output = '<a class="' . implode(' ', $classes ) . '" href="' . esc_attr( $item->url ) . '">' . $img . '</a>'; return $output; } }