I suggest creating a custom post type with the name 'Book Writer' will be a good solution.
(我建议创建一个名称为'Book Writer'的自定义帖子类型是一个很好的解决方案。)
First, create a custom post type with the name "book-writers".
(首先,创建一个名为“ book-writers”的自定义帖子类型。)
And then save all the book writers as a post. (然后将所有图书作者另存为帖子。)
Then create a custom meta-box in woo-commerce and name it "Assign a book author".
(然后在woo-commerce中创建一个自定义meta-box,并将其命名为“ Assign a book author”。)
And create a dynamic meta field as the select list in it, which will display a list of all posts of "book-writers" which are the name of the book writers. (并在其中创建一个动态的meta字段作为选择列表,该字段将显示“ book-writers”的所有帖子的列表,这些帖子即为book writers的名称。)
And save then save that meta field. (并保存然后保存该元字段。)
Before publishing the product select the book writer name you want to assign to that product and then publish the product.
(发布产品之前,选择要分配给该产品的图书作者名称,然后发布该产品。)
This way you can add authors and their relevant data of the individual author.
(这样,您可以添加作者及其与单个作者相关的数据。)
Without adding them as a user. (无需将其添加为用户。)
And you can also create a detail description page for each book writer. (您还可以为每个图书作者创建一个详细说明页面。)
I hope you understand.
(我希望你明白。)
Add the below code in your function.php
(将以下代码添加到您的function.php中)
function book_writer_meta_box(){
add_meta_box(
'book_writer_box_id', // Unique ID
'Assign a book author', // Box title
'render_book_writer_meta_box_html',
'product', // Post type
'normal',
'low'
);
}
add_action('add_meta_boxes', 'book_writer_meta_box');
function render_book_writer_meta_box_html($post){
$meta = get_post_meta( $post->ID );
wp_nonce_field( 'book_writer_metabox', 'book_writer_metabox_nonce' );
$val_bookWriter = ( isset( $meta['book_writer'][0] ) && '' !== $meta['book_writer'][0] ) ? $meta['book_writer'][0] : '';
// print_r("<p>val_autoplayTime-".$val_autoplayTime."<p/>");
echo "<div class='bookWriter_meta_fields_container'>";
echo '<p><label for="book_writer"><b>Book Writer Name: </b></label><input type="text" name="book_writer" id="book_writer" style="width:100%;" value="'. esc_attr( $val_bookWriter ) .'"></p>';
echo "</div>";
}
function book_writer_save_metadata($post_id){
// Check if our nonce is set.
if ( ! isset( $_POST['book_writer_metabox_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['book_writer_metabox_nonce'] ), 'book_writer_metabox' ) ) { // Input var okay.
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
}
else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
// OK, it's safe for us to save the data now.
// Make sure that it is set.
if ( !isset($_POST['book_writer']) ) {
return;
}
$fields = ['book_writer'];
foreach ($fields as $field) {
if (array_key_exists($field, $_POST)){
update_post_meta($post_id, $field, sanitize_text_field($_POST[$field]));
}
}
}
add_action('save_post', 'book_writer_save_metadata');
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…