// 注册职位自定义类型 function register_job_post_type() { $labels = array( 'name' => '职位', 'singular_name' => '职位', 'menu_name' => '职位管理', 'add_new' => '发布职位', 'add_new_item' => '发布新职位', 'edit_item' => '编辑职位', 'all_items' => '所有职位', ); $args = array( 'labels' => $labels, 'public' => true, 'menu_icon' => 'dashicons-briefcase', 'supports' => array('title', 'editor', 'custom-fields'), 'has_archive' => true, 'rewrite' => array('slug' => 'jobs'), ); register_post_type('job', $args); } add_action('init', 'register_job_post_type'); // 注册职位分类 function register_job_taxonomy() { $labels = array( 'name' => '职位分类', 'singular_name' => '职位分类', 'menu_name' => '分类', ); $args = array( 'labels' => $labels, 'hierarchical' => true, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array('slug' => 'job-category'), ); register_taxonomy('job_category', 'job', $args); } add_action('init', 'register_job_taxonomy'); // 添加职位字段 function add_job_meta_box() { add_meta_box( 'job_details', '职位详情', 'render_job_meta_box', 'job', 'normal', 'default' ); } add_action('add_meta_boxes', 'add_job_meta_box'); function render_job_meta_box($post) { $salary = get_post_meta($post->ID, '_job_salary', true); $location = get_post_meta($post->ID, '_job_location', true); ?>
// 保存职位字段 function save_job_meta_box_data($post_id) { if (array_key_exists('job_salary', $_POST)) { update_post_meta($post_id, '_job_salary', sanitize_text_field($_POST['job_salary'])); } if (array_key_exists('job_location', $_POST)) { update_post_meta($post_id, '_job_location', sanitize_text_field($_POST['job_location'])); } } add_action('save_post', 'save_job_meta_box_data'); // 登录限制:投递简历和发布职位 function restrict_job_access() { if (!is_user_logged_in()) { $restricted_pages = array('投递简历', '发布职位'); if (is_page($restricted_pages)) { wp_redirect(wp_login_url()); exit; } } } add_action('template_redirect', 'restrict_job_access'); // 添加单位和个人角色 function add_job_roles() { add_role('employer', '单位', array('read' => true, 'edit_posts' => true, 'publish_posts' => true)); add_role('job_seeker', '个人', array('read' => true)); } add_action('after_switch_theme', 'add_job_roles');