Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

wordpress - 我正在使用woo-commerce创建一个图书销售网站。 我想在其中使用图书作者的名字的地方。 我怎样才能做到这一点?(I am creating a book selling website using woo-commerce. where I want to use the book writer's name. how can I do that?)

I am creating a book selling website using woo-commerce.

(我正在使用woo-commerce创建一个图书销售网站。)

where I want to use the book writer's name.

(我想在其中使用图书作者的名字的地方。)

how can I do that ?

(我怎样才能做到这一点 ?)

also book writer will have some basic information.

(另外书作家也会有一些基本信息。)

its behavior like category

(其行为类似于类别)

I will add a product ( book ) and there will be an option to assign one or more book writers.Book writer will have a profile simple page as like category page.

(我将添加一个产品(book),并且将有一个选项来指派一个或多个作家。作家将拥有一个简单的个人资料页面,例如类别页面。)

在此处输入图片说明

  ask by Shakil Hossain translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...