我在自定义帖子类型分类中添加了上传图片。帖子类型为portfolio
,分类法为portfolio-category
。
输入字段和添加图像(打开wordpress uploader)有效。但是,因为我基本上从我的类别(常规帖子)复制了这个,所以有些选项不能保存。我添加了这样的表单字段:
//Category image
if ( ! function_exists( 'mytheme_image_category_field' ) ){
function mytheme_image_category_field( $tag ) {
$t_id = $tag->term_id;
$cat_meta = get_option("category_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="extra1"><?php esc_attr_e('Featured Image', 'mytheme'); ?></label></th>
<?php print_r($cat_meta, true); ?>
<td>
<div class="portfolio_category_image">
<?php if(isset($cat_meta['taxonomy_portfolio_category_image']) && $cat_meta['taxonomy_portfolio_category_image']!=''): ?>
<img width="254" src="<?php echo esc_url($cat_meta['taxonomy_portfolio_category_image']);?>" />
<?php endif; ?>
</div>
<input type="text" class="portfolio_category_image_upload" name="Cat_meta[taxonomy_portfolio_category_image]" value="<?php (isset($cat_meta['taxonomy_portfolio_category_image']) && $cat_meta['taxonomy_portfolio_category_image']!='') ? esc_attr_e($cat_meta['taxonomy_portfolio_category_image']) : '' ;?>">
<input type="button" class="button portfolio_upload_image_button" name="image_upload" value=" <?php esc_attr_e('Upload Image', 'mytheme');?>">
<input type="button" class="button portfolio_remove_image_button" name="remove_image_upload" value=" <?php esc_attr_e('Remove Image', 'mytheme');?>">
</td>
</tr>
<?php
}
}
add_action ( 'portfolio-category_edit_form_fields', 'mytheme_image_category_field');
if ( ! function_exists( 'mytheme_extra_add_image_upload_field' ) ){
function mytheme_extra_add_image_upload_field( $tag ) {
$t_id = (is_object($tag)) ? $tag->term_id:'';
$cat_meta = get_option("category_$t_id");
?>
<div class="form-field">
<label for="extra1"><?php esc_attr_e('Featured Image', 'mytheme'); ?></label>
<input type="text" class="portfolio_category_image_upload" name="Cat_meta[taxonomy_portfolio_category_image]" value="<?php echo (isset($cat_meta['taxonomy_portfolio_category_image']) && $cat_meta['taxonomy_portfolio_category_image'] != '') ? esc_attr($cat_meta['taxonomy_portfolio_category_image']) : '' ;?>">
<input type="button" class="button portfolio_upload_image_button" name="Cat_meta[taxonomy_portfolio_category_image]" value=" <?php esc_attr_e('Upload Image', 'mytheme');?>">
<input type="button" class="button portfolio_remove_image_button" name="remove_image_upload" value=" <?php esc_attr_e('Remove Image', 'mytheme');?>">
</div>
<?php
}
}
add_action ( 'portfolio-category_add_form_fields', 'mytheme_extra_add_image_upload_field');
明显的问题是,在自定义帖子类型中,我没有在wp_options
表格中写任何内容,因此$cat_meta = get_option("category_$t_id");
将无法获得任何内容。
当我回复$t_id
时,我得到的是我的术语ID。所以我知道我需要以某种方式使用terms
来保存我的选项。但这里是我被困的地方。 get_terms('portfolio-category')
会正确输出我拥有的每个字词,但我没有上传的图像。
javascript是(如果有人需要测试的话):
$(document).on('click', '.portfolio_upload_image_button', portfolio_upload_image_button);
function portfolio_upload_image_button(e) {
e.preventDefault();
var $input_field = $(this).prev();
var $image = $('.portfolio_category_image');
var custom_uploader = wp.media.frames.file_frame = wp.media({
title: 'Add Image',
button: {
text: 'Add Image'
},
multiple: false
});
custom_uploader.on('select', function() {
var attachment = custom_uploader.state().get('selection').first().toJSON();
$input_field.val(attachment.url);
$image.html('<img width="254" src="'+attachment.url+'" />');
});
custom_uploader.open();
}
$(document).on('click', '.portfolio_remove_image_button', portfolio_remove_image_button);
function portfolio_remove_image_button(e){
e.preventDefault();
var $input_field = $('.portfolio_category_image_upload');
var $image = $('.portfolio_category_image');
$input_field.val('');
$image.html('');
}
这适用于我的常规帖子,但不适用于我的CPT。
修改
我应该使用wp_insert_term吗?要更新我的分类术语吗?
答案 0 :(得分:0)
class Category_Taxonomy_Image{
function __construct(){
add_action( 'portfolio-category_add_form_fields', array( $this, 'add_tax_image_field' ) );
add_action( 'portfolio-category_edit_form_fields', array( $this, 'edit_tax_image_field' ) );
// saving
add_action( 'edited_portfolio-category', array( $this, 'save_tax_meta' ), 10, 2 );
add_action( 'create_portfolio-category', array( $this, 'save_tax_meta' ), 10, 2 );
}
public function add_tax_image_field(){
?>
<div class="form-field">
<label for="term_meta[tax_image]"><?php esc_html_e('Category Featured Image', 'mytheme'); ?></label>
<input type="text" class="portfolio_category_image_upload" name="term_meta[tax_image]" id="term_meta[tax_image]" value="" />
<input type="button" class="button portfolio_upload_image_button" name="image_upload" value=" <?php esc_attr_e('Upload Image', 'mytheme');?>">
<input type="button" class="button portfolio_remove_image_button" name="remove_image_upload" value=" <?php esc_attr_e('Remove Image', 'mytheme');?>">
<p class="description"><?php esc_html_e('Add URL to image for the category image', 'mytheme'); ?></p>
</div><!-- /.form-field -->
<?php
}
public function edit_tax_image_field( $term ){
$term_id = $term->term_id;
$term_meta = get_option( "taxonomy_$term_id" );
$image = $term_meta['tax_image'] ? $term_meta['tax_image'] : '';
?>
<tr class="form-field">
<th scope="row">
<label for="term_meta[tax_image]"><?php esc_html_e('Category Featured Image', 'mytheme'); ?></label>
<td>
<div class="portfolio_category_image">
<?php if(isset($image) && $image!=''): ?>
<img width="254" src="<?php echo esc_url($image);?>" />
<?php endif; ?>
</div>
<input type="text" class="portfolio_category_image_upload" name="term_meta[tax_image]" id="term_meta[tax_image]" value="<?php echo esc_url( $image ); ?>" />
<input type="button" class="button portfolio_upload_image_button" name="image_upload" value=" <?php esc_attr_e('Upload Image', 'mytheme');?>">
<input type="button" class="button portfolio_remove_image_button" name="remove_image_upload" value=" <?php esc_attr_e('Remove Image', 'mytheme');?>">
<p class="description"><?php esc_html_e('Add URL to image for the category image', 'mytheme'); ?></p>
</td>
</th>
</tr>
<?php
}
public function save_tax_meta( $term_id ){
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = array();
$term_meta['tax_image'] = isset ( $_POST['term_meta']['tax_image'] ) ? esc_url( $_POST['term_meta']['tax_image'] ) : '';
update_option( "taxonomy_$t_id", $term_meta );
}
}
}
$cat_tax_image = new Category_Taxonomy_Image();
密钥是将它们保存到wp_options
表并添加到正确的挂钩。主要是:
add_action( '{custom-taxonomy-name}_add_form_fields', 'add_new_custom_fields');
add_action( '{custom-taxonomy-name}_edit_form_fields', 'add_edit_custom_fields');
并且
add_action( 'create_{custom-taxonomy-name}', 'save_custom_taxonomy_meta');
add_action( 'edited_{custom-taxonomy-name}', 'save_custom_taxonomy_meta');