wordpress在后台多添加指定的分类选择区块

wordpress在后台多添加指定的分类选择区块

在WordPress后台为文章编辑页面添加额外的分类选择区块,可以通过添加自定义meta box来实现。以下是一个示例代码,演示如何添加一个多选分类的meta box。

// 在functions.php或者你的插件文件中添加以下代码
 
// 添加自定义meta box
function add_custom_taxonomies_meta_box() {
    add_meta_box('custom-taxonomies-meta-box', '额外分类', 'render_custom_taxonomies_meta_box', 'post', 'side', 'high');
}
add_action('add_meta_boxes', 'add_custom_taxonomies_meta_box');
 
// 渲染自定义meta box的内容
function render_custom_taxonomies_meta_box($post) {
    // 获取已有分类关联
    $terms = get_the_terms($post->ID, 'category');
    $selected_terms = $terms ? array_keys(wp_list_pluck($terms, 'term_id')) : array();
    // 获取所有分类
    $all_terms = get_terms(array(
        'taxonomy' => 'category',
        'hide_empty' => false
    ));
    // 渲染多选框
    echo '<select multiple="multiple" name="extra_categories[]" id="extra_categories">';
    foreach ($all_terms as $term) {
        echo '<option value="' . $term->term_id . '" ' . (in_array($term->term_id, $selected_terms) ? 'selected' : '') . '>' . $term->name . '</option>';
    }
    echo '</select>';
}
 
// 保存自定义meta box数据
function save_custom_taxonomies_meta_box_data($post_id) {
    // 确保非管理员用户的非自动保存请求
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    if (!current_user_can('edit_post', $post_id)) return;
 
    // 更新分类关联
    if (isset($_POST['extra_categories'])) {
        $extra_categories = array_map('intval', $_POST['extra_categories']);
        wp_set_object_terms($post_id, $extra_categories, 'category');
    }
}
add_action('save_post', 'save_custom_taxonomies_meta_box_data');

将上述代码放入你的WordPress主题的functions.php文件中,或者如果你在编写插件,则可以将代码放入插件的主文件中。这段代码将会在文章编辑页面的侧边栏添加一个名为“额外分类”的meta box,允许用户选择多个分类。选择的分类将与文章关联,并保存在文章的元数据中。

提示:仅供参考

方法二:

好的,基于您的需求,我将为您提供一个WordPress的代码示例,这段代码将帮助您在后台的文章编辑页面添加指定分类ID(例如ID为6)下的所有子分类的区块。

以下是一个简单的PHP代码片段,您可以将其添加到您的WordPress插件或主题的functions.php文件中:

function add_custom_categories_meta_box() {
    add_meta_box(
        'custom_categories_meta_box', // Meta box ID
        '额外分类', // Meta box title
        'display_custom_categories_meta_box', // Callback function to display the meta box content
        'post', // Post type to add the meta box to
        'normal', // Context where the meta box should show ('normal', 'advanced', or 'side')
        'high' // Priority within the context where the meta box should show ('high', 'core', 'default' or 'low')
    );
}

function display_custom_categories_meta_box($post) {
    $args = array(
        'child_of' => 6, // 指定父分类ID
        'hide_empty' => 0, // 是否隐藏没有文章的分类
        'taxonomy' => 'category' // 分类法名称
    );
    
    $categories = get_categories($args); // 获取指定分类ID下的所有子分类
    
    foreach ($categories as $category) {
        echo '<label><input type="checkbox" name="custom_categories[]" value="' . $category->term_id . '"> ' . $category->name . '</label><br>';
    }
}

add_action('add_meta_boxes', 'add_custom_categories_meta_box'); // 挂钩到add_meta_boxes动作上

这段代码首先定义了一个add_custom_categories_meta_box函数,该函数使用add_meta_box函数在WordPress后台的文章编辑页面添加一个自定义的meta box。然后,display_custom_categories_meta_box函数负责显示这个meta box的内容,它会列出指定分类ID(在这个例子中是6)下的所有子分类,并为每个子分类提供一个复选框。

希望这段代码能满足您的需求。如果您有任何其他问题或需要进一步的帮助,请随时告诉我。

方法二修改:增加数据保存,没有和默认的分类一起

function add_custom_categories_meta_box() {
    add_meta_box(
        'custom_categories_meta_box',
        '额外分类',
        'display_custom_categories_meta_box',
        'post',
        'normal',
        'high'
    );
}

function display_custom_categories_meta_box($post) {
    $args = array(
        'child_of' => 6,
        'hide_empty' => 0,
        'taxonomy' => 'category'
    );

    $categories = get_categories($args);

    $selected_categories = get_post_meta($post->ID, '_custom_categories', true);

    foreach ($categories as $category) {
        $checked = in_array($category->term_id, $selected_categories)? 'checked' : '';
        echo '<label><input type="checkbox" name="custom_categories[]" value="'. $category->term_id. '" '. $checked. '> '. $category->name. '</label><br>';
    }
}

function save_custom_categories_meta_box_data($post_id) {
    if (isset($_POST['custom_categories'])) {
        $selected_categories = array_map('intval', $_POST['custom_categories']);
        update_post_meta($post_id, '_custom_categories', $selected_categories);
    } else {
        delete_post_meta($post_id, '_custom_categories');
    }
}

add_action('add_meta_boxes', 'add_custom_categories_meta_box');
add_action('save_post', 'save_custom_categories_meta_box_data');