I'm using option tree for my theme options page. But I can't retrieve uploaded images attribute like alternative text, description,caption etc.
How can I do that.
I'm using option tree for my theme options page. But I can't retrieve uploaded images attribute like alternative text, description,caption etc.
How can I do that.
Put this code in index.php
<img class="img-resposive" src="<?php
$logo=get_option_tree( 'logo','','true'); // return src of img
$id_logo = get_attachment_id_from_src($logo); // This is custom function for getting image id.
$alt = get_post_meta($id_logo, '_wp_attachment_image_alt', true);// get alt="" from wordpress media.
if ( function_exists( 'get_option_tree') ) :
if( get_option_tree( 'logo')) :
$logo;
else:
echo bloginfo('template_directory') .'/assets/images/logo_vp.png'; // else if option is empty get this image.
endif;
endif;
?>" alt="<?php echo $alt; ?>"/>
Put this function code in functions.php
//get id from image source
function get_attachment_id_from_src ($image_src) {
global $wpdb;
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
$id = $wpdb->get_var($query);
return $id;
}
This code is tested. Working fine.
Here is the correct answer for optiontree 2.4+
https://github.com/valendesigns/option-tree/issues/112
Note
'class' => 'ot-upload-attachment-id',
Example:
array(
'id' => 'demo_upload_attachment_id',
'label' => __( 'Upload Attachment ID', 'option-tree-theme' ),
'desc' => sprintf( __( 'The Upload option type can also be saved as an attachment ID by adding %s to the class attribute.', 'option-tree-theme' ), '<code>ot-upload-attachment-id</code>' ),
'std' => '',
'type' => 'upload',
'section' => 'option_types',
'rows' => '',
'post_type' => '',
'taxonomy' => '',
'min_max_step'=> '',
'class' => 'ot-upload-attachment-id',
'condition' => '',
'operator' => 'and'
)