此代码添加到您的functions.php或创建一个新的插件:
// action call to register a taxonomy
add_action('init', 'artist_to_product');
// function for registering artist taxonomy
function artist_to_product() {
$labels = array(
'name' => 'Artists',
'singular_name' => 'Artist',
'menu_name' => 'Artist',
'all_items' => 'All Artists',
'new_item_name' => 'New Artist Name',
'add_new_item' => 'Add New Artist',
'edit_item' => 'Edit Artist',
'update_item' => 'Update Item',
'separate_items_with_commas' => 'Separate Artists with commas',
'search_items' => 'Search Artists',
'add_or_remove_items' => 'Add or remove Artists',
'choose_from_most_used' => 'Choose from the most used Artists',
);
$args = array(
'labels' => $labels,
'hierarchical' => false, // we dont need hierarchical artists
'public' => true,
'show_ui' => true, // you can set it to false if you want
'show_admin_column' => true, // you can set it to false if you want
'show_in_nav_menus' => true, // show in product menu bar?
'show_tagcloud' => true, // you can set it to false if you want
);
// register the taxonomys
register_taxonomy('artist', 'product', $args);
register_taxonomy_for_object_type('artist', 'product');
}
/**
* You have now an ARTIST taxonomy in your products
* Your taxonomy link: http://www.yourdomain.com/artist/the-artist-name/
* Dont forget to flush your permalinks: Settings->Permalinks and save as it is.
* You can add manuel an artist or do it automaticly
* for automaticly adding on save/update a product
*/
/**
* Save post metadata when a post is saved.
*
* @param int $post_id The post ID.
* @param post $post The post object.
* @param bool $update Whether this is an existing post being updated or not.
*/
function save_artist_meta($post_id, $post, $update) {
/*
* In production code, $slug should be set only once in the plugin,
* preferably as a class property, rather than in each function that needs it.
*/
$post_type = get_post_type($post_id);
// If this isn't a 'product' post, don't update it.
if ("product" != $post_type) return;
// - Update the post's taxonomy.
// GET YOUR ARTIST NAME FROM IMAGE
// i dont know how you retrierve it
// is it from thumbnail or from other images? i dont know
//Example:
$artist_name = 'Leonardo Da Vinci'; // can be comma seperated like: 'Leonardo Da Vinci, Van Gogh' or an array of artists
// set taxonomy to post
// make a check
if(empty($artist_name)) return;
$taxonomy = 'artist';
$append = false; // we dont want to append, we want to replace it.
wp_set_object_terms($post_id, $artist_name, $taxonomy, $append);
}
add_action('save_post', 'save_artist_meta', 99, 3);
如何获得针对前端的分类法:
// wp function to get the artists from a post as object
$post_id = 46772; // your post id or post object
$taxonomy = 'artist';
$artists_from_post = get_the_terms($post_id, $taxonomy);
// wp function to get the artists url
$taxonomy_slug = 'deny-de-vito'; // the slug of your artist taxonomy from above
$taxonomy = 'artist';
$artist_url = get_term_link($taxonomy_slug, $taxonomy);
现在,如何找到一个蛞蝓只有纯文本/艺术家的名字? 如果通过此功能全自动保存艺术家的名字,所以我们可以使用WP sanitize_title功能:
$artist_name_from_image = 'Leonardo Da Vinci';
$artist_slug = sanitize_title($artist_name_from_image);
// this wil produce: 'leonardo-da-vinci'.
迈克,谢谢你帮助我。这是你代表我做的一件令人印象深刻的工作。奇怪的是,我刚刚得到了你的答案,我确实已经检查过。我会尽快研究并尝试您的解决方案,然后马上回来。我明天和星期三确实有一系列的约会,但我很好奇,在星期四之前我会尝试回复。现在,再次感谢你,我让你知道。 – A3O
这是令人难以置信的!这工作真的很棒。如预期的那样更多!你摇滚!不仅如此,我还在仪表板中看到了艺术家的名字 - >产品标签。超!!!真。但是,当我们走到这么远时,在测试这个解决方案时想到了几件小事。因为现在我无法为艺术家搜索产品的数据库。 Wordpress提出“找不到产品”;第二,我使用WPML进行transltion管理,我发现这种分类法没有用其他语言填写。 – A3O
通常情况下,您可以在分类标签中看到它是否被复制,翻译或无所事事选项。现在它被设置为“无所事事”,因此在其他语言中没有关于“艺术家”分类的数据。如果ther是一个将此分类法复制到每种语言的解决方案,但复制选项不可见,那将是非常好的。我将介绍WPML设置以实现这一目标。最后我不明白你提供的最后3位代码。我在我的函数中评论过它.php和idt很有用。 – A3O