Wordpress 管理面板中的自定义分类图像选项
我已经创建了一个自定义分类法,并希望每个类别都有一个图像选项Wordpress 管理面板中的自定义分类图像选项,wordpress,custom-taxonomy,Wordpress,Custom Taxonomy,我已经创建了一个自定义分类法,并希望每个类别都有一个图像选项 function create_product_taxonomies() { // Add new taxonomy, make it hierarchical (like categories) $labels = array( 'name' => _x('product_categories', 'taxonomy general name'), 'singular
function create_product_taxonomies()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x('product_categories', 'taxonomy general name'),
'singular_name' => _x('Product', 'taxonomy singular name'),
'search_items' => __('Search Product Category'),
'all_items' => __('All Product Categorie(s)'),
'parent_item' => __('Parent Product Category'),
'parent_item_colon' => __('Parent Product Category:'),
'edit_item' => __('Edit Product Category'),
'update_item' => __('Update Product Category'),
'add_new_item' => __('Add New'),
'new_item_name' => __('New Product Name'),
'menu_name' => __('Product Categories'),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'product_categories', 'with_front' => true));
register_taxonomy('product_categories', array('products'), $args);
flush_rewrite_rules();
}
//hooks
add_action('init', 'create_product_taxonomies', 0);
我如何向用户提供为每个类别上载图像的选项?无法添加全部代码,但您可以获得它
register_field_group( array (
'id' => 'acf_taxonomy-authors',
'title' => 'Taxonomy Authors',
'fields' => array (
array (
'key' => 'field_560746011af90',
'label' => 'Book Authors',
'name' => 'book_authors',
'type' => 'image',
'instructions' => 'Upload the book author image here',
'save_format' => 'url',
'preview_size' => 'thumbnail',
'library' => 'all',
),
),
'location' => array (
array (
array (
'param' => 'ef_taxonomy',
'operator' => '==',
'value' => 'author',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'default',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
税务登记:
<?php
/**
* @uses Register Book Author Taxonomy
* @uses Adds query vars
* @author Nirpendra Patel
* @return void
**/
function reg_tax_author() {
$authors = array(
'name' => _x( 'Authors', 'Taxonomy plural name', 'b_thebookstore' ),
'singular_name' => _x( 'Author', 'Taxonomy singular name', 'b_thebookstore' ),
'search_items' => __( 'Search Authors', 'b_thebookstore' ),
'popular_items' => __( 'Popular Authors', 'b_thebookstore' ),
'all_items' => __( 'All Authors', 'b_thebookstore' ),
'parent_item' => __( 'Parent Author', 'b_thebookstore' ),
'parent_item_colon' => __( 'Parent Author:', 'b_thebookstore' ),
'edit_item' => __( 'Edit Author', 'b_thebookstore' ),
'update_item' => __( 'Update Author', 'b_thebookstore' ),
'add_new_item' => __( 'Add New Author', 'b_thebookstore' ),
'new_item_name' => __( 'New Author Name', 'b_thebookstore' ),
'add_or_remove_items' => __( 'Add or remove Authors', 'b_thebookstore' ),
'choose_from_most_used' => __( 'Choose from most used Authors', 'b_thebookstore' ),
'menu_name' => __( 'Authors', 'b_thebookstore' ),
);
$args = array(
'labels' => $authors,
'public' => true,
'show_in_nav_menus' => true,
'show_admin_column' => true,
'hierarchical' => true,
'show_tagcloud' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'query_var' => true,
'capabilities' => array(),
);
register_taxonomy( 'author', array( 'product' ), $args );
}
?>
在WordPress v4.4.2之前,为术语创建自定义元字段非常困难,但不可能,但现在非常简单 您想要上传图像,所以我们创建了一个js文件,并在
functions.php
中编写了一些代码
但首先我们要创建自定义上传元字段
在function.php
中,或者在其中编写注册自定义分类的代码并编写以下代码:
首先,我们创建自定义元术语字段
如上所述,在添加操作中的created\u类别
现在我们为编辑创建元术语字段
现在我们转到WordPress媒体上传程序的**JS文件**
media-uploader.js
jQuery(document).ready(function($){
// Instantiates the variable that holds the media library frame.
var meta_image_frame;
// Runs when the image button is clicked.
$('#upload_image_btn').click(function(e){
// Prevents the default action from occuring.
e.preventDefault();
// If the frame already exists, re-open it.
if ( meta_image_frame ) {
meta_image_frame.open();
return;
}
// Sets up the media library frame
meta_image_frame = wp.media.frames.meta_image_frame = wp.media({
title: meta_image.title,
button: { text: meta_image.button },
library: { type: 'image' }
});
// Runs when an image is selected.
meta_image_frame.on('select', function(){
// Grabs the attachment selection and creates a JSON representation of the model.
var media_attachment = meta_image_frame.state().get('selection').first().toJSON();
// Sends the attachment URL to our custom image input field.
$('#txt_upload_image').val(media_attachment.url);
});
// Opens the media library frame.
meta_image_frame.open();
});
});
现在是最后一步
转到functions.php
function image_uploader_enqueue() {
global $typenow;
if( ($typenow == 'products') ) {
wp_enqueue_media();
wp_register_script( 'meta-image', get_template_directory_uri() . '/js/media-uploader.js', array( 'jquery' ) );
wp_localize_script( 'meta-image', 'meta_image',
array(
'title' => 'Upload an Image',
'button' => 'Use this Image',
)
);
wp_enqueue_script( 'meta-image' );
}
}
add_action( 'admin_enqueue_scripts', 'image_uploader_enqueue' );
调用图像
调用图像
">
这是我的工作代码,请小心使用
希望这段代码能对你有所帮助。如果这段代码有效,请接受我的回答:)你现在的WordPress版本是最新版本请告诉我号码,例如3.658.254好的……等待至少30分钟,你需要连接到
产品类别编辑表单字段和产品类别添加表单字段
,并添加自定义字段上载图像字段并保存它们,以及
add_action('product_categories_edit_form_fields', 'edit_image_upload', 10, 2);
function edit_image_upload($term, $taxonomy) {
// get current group
$txt_upload_image = get_term_meta($term->term_id, 'term_image', true);
?>
<div class="form-field term-group">
<label for="">Upload and Image</label>
<input type="text" name="txt_upload_image" id="txt_upload_image" value="<?php echo $txt_upload_image ?>" style="width: 77%">
<input type="button" id="upload_image_btn" class="button" value="Upload an Image" />
</div>
<?php
}
add_action('edited_product_categories', 'update_image_upload', 10, 2);
function update_image_upload($term_id, $tt_id) {
if (isset($_POST['txt_upload_image']) && '' !== $_POST['txt_upload_image']){
$group = '#' . sanitize_title($_POST['txt_upload_image']);
update_term_meta($term_id, 'term_image', $group);
}
}
jQuery(document).ready(function($){
// Instantiates the variable that holds the media library frame.
var meta_image_frame;
// Runs when the image button is clicked.
$('#upload_image_btn').click(function(e){
// Prevents the default action from occuring.
e.preventDefault();
// If the frame already exists, re-open it.
if ( meta_image_frame ) {
meta_image_frame.open();
return;
}
// Sets up the media library frame
meta_image_frame = wp.media.frames.meta_image_frame = wp.media({
title: meta_image.title,
button: { text: meta_image.button },
library: { type: 'image' }
});
// Runs when an image is selected.
meta_image_frame.on('select', function(){
// Grabs the attachment selection and creates a JSON representation of the model.
var media_attachment = meta_image_frame.state().get('selection').first().toJSON();
// Sends the attachment URL to our custom image input field.
$('#txt_upload_image').val(media_attachment.url);
});
// Opens the media library frame.
meta_image_frame.open();
});
});
function image_uploader_enqueue() {
global $typenow;
if( ($typenow == 'products') ) {
wp_enqueue_media();
wp_register_script( 'meta-image', get_template_directory_uri() . '/js/media-uploader.js', array( 'jquery' ) );
wp_localize_script( 'meta-image', 'meta_image',
array(
'title' => 'Upload an Image',
'button' => 'Use this Image',
)
);
wp_enqueue_script( 'meta-image' );
}
}
add_action( 'admin_enqueue_scripts', 'image_uploader_enqueue' );
<?php
$categories = get_terms('product_categories');
foreach($categories as $term) {
$upload_image = get_term_meta($term->term_id, 'term_image', true);
?>
<img src="<?php echo $upload_image ?>">
<?php
}
?>