th.column-thumb {width:60px;}
.form-field img.taxonomy-image {border:1px solid #eee;max-width:300px;max-height:300px;}
.inline-edit-row fieldset .thumb label span.title {width:48px;height:48px;border:1px solid #eee;display:inline-block;}
.column-thumb span {width:48px;height:48px;border:1px solid #eee;display:inline-block;}
.inline-edit-row fieldset .thumb img,.column-thumb img {width:48px;height:48px;}
';
}
// 添加在添加窗体中的图像字段
function z_add_texonomy_field() {
if (get_bloginfo('version') >= 3.5) // worpdress 最低版本3.5
wp_enqueue_media();
else {
wp_enqueue_style('thickbox');
wp_enqueue_script('thickbox');
}
echo '
'.z_script();
}
// 在编辑窗体中添加图像字段
function z_edit_texonomy_field($taxonomy) {
if (get_bloginfo('version') >= 3.5)
wp_enqueue_media();
else {
wp_enqueue_style('thickbox');
wp_enqueue_script('thickbox');
}
if (z_taxonomy_image_url( $taxonomy->term_id, NULL, TRUE ) == Z_IMAGE_PLACEHOLDER)
$image_url = "";
else
$image_url = z_taxonomy_image_url( $taxonomy->term_id, NULL, TRUE );
echo '
|
|
'.z_script();
}
// 上传图像
function z_script() {
return '';
}
// 添加到钩子
add_action('edit_term','z_save_taxonomy_image');
add_action('create_term','z_save_taxonomy_image');
function z_save_taxonomy_image($term_id) {
if(isset($_POST['taxonomy_image']))
update_option('z_taxonomy_image'.$term_id, $_POST['taxonomy_image'], NULL);
}
// 附件地址
function z_get_attachment_id_by_url($image_src) {
global $wpdb;
$query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid = %s", $image_src);
$id = $wpdb->get_var($query);
return (!empty($id)) ? $id : NULL;
}
// 我不知道
function z_taxonomy_image_url($term_id = NULL, $size = 'full', $return_placeholder = FALSE) {
if (!$term_id) {
if (is_category())
$term_id = get_query_var('cat');
elseif (is_tag())
$term_id = get_query_var('tag_id');
elseif (is_tax()) {
$current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
$term_id = $current_term->term_id;
}
}
$taxonomy_image_url = get_option('z_taxonomy_image'.$term_id);
if(!empty($taxonomy_image_url)) {
$attachment_id = z_get_attachment_id_by_url($taxonomy_image_url);
if(!empty($attachment_id)) {
$taxonomy_image_url = wp_get_attachment_image_src($attachment_id, $size);
$taxonomy_image_url = $taxonomy_image_url[0];
}
}
if ($return_placeholder)
return ($taxonomy_image_url != '') ? $taxonomy_image_url : Z_IMAGE_PLACEHOLDER;
else
return $taxonomy_image_url;
}
function z_quick_edit_custom_box($column_name, $screen, $name) {
if ($column_name == 'thumb')
echo '';
}
/**
* 添加类别管理
*/
function z_taxonomy_columns( $columns ) {
$new_columns = array();
if(isset($columns)){
$new_columns['cb'] = $columns['cb'];
unset( $columns['cb'] );
}
$new_columns['thumb'] = __('image', 'sakurairo')/*图像*/;
return array_merge( $new_columns, $columns );
}
/**
* 图像列表
*/
function z_taxonomy_column( $columns, $column, $id ) {
if ( $column == 'thumb' )
$columns = '';
return $columns;
}
// 将“插入帖子”更改为“使用此图片”
function z_change_insert_button_text($safe_text, $text) {
return str_replace("Insert into Post", "Use this image", $text);
}
// 列表图片样式
if ( strpos( $_SERVER['SCRIPT_NAME'], 'edit-tags.php' ) > 0 ) {
add_action('quick_edit_custom_box', 'z_quick_edit_custom_box', 10, 3);
add_filter("attribute_escape", "z_change_insert_button_text", 10, 2);
}