从选项树主题选项中检索上传的图像属性值

时间:2016-02-20 作者:Porosh Ahammed

我正在为我的主题选项页面使用选项树。但我无法检索上传的图像属性,如可选文本、描述、标题等。

我该怎么做呢。

2 个回复
最合适的回答,由SO网友:asadadin 整理而成

输入此代码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; ?>"/>
输入此功能代码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;
}
此代码已测试。工作正常。

SO网友:Alireza

以下是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\'
)

相关推荐