将WordPress自定义分类类别分配到自定义文章类型中

jum4pzuy  于 2023-11-17  发布在  WordPress
关注(0)|答案(1)|浏览(149)

我一直在为一个自定义的形式,插入后到自定义后在wordpress的类型工作。我需要分配这个职位的自定义分类类别。
以下是注册的自定义分类-

// Portfolio Category
$portfolio_cat_tax = array(
    'name' => 'Portfolio Category',
    'singular_name' => 'Portfolio Category',
    'search_items' => 'Search Category',
    'all_items' => 'All Category',
    'edit_item' => 'Edit Category',
    'update_item' => 'Update Category',
    'add_new_item' => 'Add New Category',
    'new_item_name' => 'New Category',
    'menu_name' => 'Portfolio Category',
);

$portfolio_cat_tax_args = array(
    'hierarchical' => true,
    'labels' => $portfolio_cat_tax,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => 'portfolio_category',
    'rewrite' => array('slug' => 'portfolio-category'),
);

register_taxonomy('portfolio_category', 'portfolio', $portfolio_cat_tax_args);

字符串
这里是自定义文章类型注册-

// Portfolio post type
$khs_portfolio_labels = array(
    'name'                  => __( 'Portfolio', 'khs' ),
    'singular_name'         => __( 'Portfolio', 'khs' ),
    'menu_name'             => __( 'Portfolio', 'khs' ),
    'name_admin_bar'        => __( 'Portfolio', 'khs' ),
    'add_new'               => __( 'Add Portfolio', 'khs' ),
    'add_new_item'          => __( 'New Portfolio', 'khs' ),
    'new_item'              => __( 'New Portfolio', 'khs' ),
    'edit_item'             => __( 'Edit Portfolio', 'khs' ),
);

$khs_portfolio_args = array(
    'labels'             => $khs_portfolio_labels,
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'portfolio' ),
    'has_archive'        => false,
    'hierarchical'       => true,
    'menu_icon'          => 'dashicons-portfolio',
    'menu_position'      => null,
    'supports'           => array( 'title', 'thumbnail' ),
    'taxonomies' => array('portfolio_category'),
    
);

register_post_type('portfolio', $khs_portfolio_args);


下面是我尝试过的表单数据提交代码-

// Handle Submit Portfolio
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["agency_portfolio_submit"])) {
    $khs_project_name = sanitize_text_field($_POST["project_name"]);
    $khs_portfolio_category = $_POST["selected_agency_portfolio_category"];
    $khs_portfolio_link = sanitize_text_field($_POST["portfolio_link"]);

    // Insert the post
    $khs_portfolio_post = array(
        'post_title'    => $khs_project_name,
        'post_status'   => 'publish',
        'post_author'   => get_current_user_id(),
        'post_type'     => 'portfolio',
        'meta_input'    => [
            'portfolio_link'  => $khs_portfolio_link,
            'selected_agency_portfolio_category'    =>  $khs_portfolio_category
        ],
        'tax_input'     => array(
            'portfolio_category' => $khs_portfolio_category,
        ),
    );

    $khs_portfolio_post_id = wp_insert_post($khs_portfolio_post);
    wp_set_object_terms($khs_portfolio_post_id, $khs_portfolio_category, "portfolio-category");

    // Handle the thumbnail upload
    if ($_FILES['portfolio_thumbnail']['size'] > 0) {
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        require_once(ABSPATH . 'wp-admin/includes/file.php');
        require_once(ABSPATH . 'wp-admin/includes/media.php');
        
        $attachment_id = media_handle_upload('portfolio_thumbnail', $khs_portfolio_post_id);
        set_post_thumbnail($khs_portfolio_post_id, $attachment_id);
    }
}


我已经尝试了tax_inputwp_set_object_termswp_set_post_terms,但它对我不起作用。$khs_portfolio_category返回一个分类ID数组。
自定义文章类型和自定义分类功能或其他功能是否有任何混乱?
我也试过一些stackoverflow和它的相关网络网站在这里-
StackOverflow
Wordpress StackExchange
但是它没有给文章分配分类类别。

4xrmg8kj

4xrmg8kj1#

1.使用动作挂钩对每个配准进行排序。
1.将register_taxonomy()排序为在register_post_type()之前执行。
1.调用register_taxonomy()时,将$post_type参数设置为 null

使用action钩子

例如,对$portfolio_cat_tax$portfolio_cat_tax_args的赋值以及对register_taxonomy()的调用都应该包含在一个函数中。该函数充当init操作钩子的处理程序。操作钩子必须设置register_taxonomy()的优先级,以便在register_post_type()之前执行。

示例:

// Portfolio Category
function register_my_custom_taxonomy() {
    $portfolio_cat_tax = array(
        'name' => 'Portfolio Category',
        'singular_name' => 'Portfolio Category',
        'search_items' => 'Search Category',
        'all_items' => 'All Category',
        'edit_item' => 'Edit Category',
        'update_item' => 'Update Category',
        'add_new_item' => 'Add New Category',
        'new_item_name' => 'New Category',
        'menu_name' => 'Portfolio Category',
    );

    $portfolio_cat_tax_args = array(
        'hierarchical' => true,
        'labels' => $portfolio_cat_tax,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => 'portfolio_category',
        'rewrite' => array('slug' => 'portfolio-category'),
    );

    register_taxonomy('portfolio_category', null, $portfolio_cat_tax_args);
}
add_action( 'init', 'register_my_custom_taxonomy', 10 );

个字符

相关问题