Skip to content

Instantly share code, notes, and snippets.

@ssbalakumar
Created December 15, 2017 09:09
Show Gist options
  • Save ssbalakumar/0e1fddcbc670637be6e819f80a8dd990 to your computer and use it in GitHub Desktop.
Save ssbalakumar/0e1fddcbc670637be6e819f80a8dd990 to your computer and use it in GitHub Desktop.
Custom Post Type Jobs Careers
// Custom Post Types
function wpt_job_post_type() {
$labels = array(
'name' => 'Vacancies',
'singular_name' => 'Job Opportunity',
'add_new' => 'Add New Job',
'add_new_item' => 'Add New Job Opportunity',
'edit_item' => 'Edit Job',
'new_item' => 'New Job Opportunity',
'view_item' => 'View Job',
'search_items' => 'Search Careers',
'not_found' => 'No jobs found',
'not_found_in_trash' => 'No jobs found in Trash',
'parent_item_colon' => 'Parent Job:'
);
$supports = array(
'title',
'editor',
'thumbnail',
'comments',
'revisions',
);
$args = array(
'labels' => $labels,
'supports' => $supports,
'public' => true,
'exclude_from_search' => false,
'map_meta_cap'=> true,
'capability_type' => 'post',
'rewrite' => array(
'slug' => 'vacancies',
'with_front' => true,
'pages' => true,
'feeds' => 'careers'
),
'has_archive' => true,
'menu_position' => 30,
'menu_icon' => 'dashicons-businessman',
'taxonomies' => array('job-category'),
);
register_post_type( 'vacancies', $args );
}
add_action( 'init', 'wpt_job_post_type' );
register_taxonomy( 'job-category', array('vacancies'),
array(
'hierarchical' => true,
'rewrite' => array('slug' => 'vacancies/job-category'),
'query_var' => '',
'public' => true,
'show_ui' => true,
'show_tagcloud' => true,
'labels' => array(
'name' => 'Job Categories',
'singular_name' => 'Job Category',
'search_items' => 'Search Job Categories',
'popular_items' => 'Popular Job Categories',
'all_items' => 'All Job Categories',
'parent_item' => 'Parent Job Category',
'parent_item_colon' => 'Parent Job Category:',
'edit_item' => 'Edit Job Category',
'update_item' => 'Update Job Category',
'add_new_item' => 'Add New Job Category',
'new_item_name' => 'New Job Category Name',
'separate_items_with_commas' => 'Separate job categories with commas',
'add_or_remove_items' => 'Add or remove job categories',
'choose_from_most_used' => 'Choose from the most used job categories'
),
'capabilities' => array(
'manage_terms' => 'manage_categories',
'edit_terms' => 'manage_categories',
'delete_terms' => 'manage_categories',
'assign_terms' => 'edit_posts'
),
'show_in_nav_menus' => true
)
);
// Preload taxonomy with some common terms
$job_category_preload_terms = array(
'Nurse',
'Civil Engineer',
'Electrician'
);
foreach($job_category_preload_terms as $job_category_term){
wp_insert_term($job_category_term, 'job-category');
}
add_filter('single_template','job_single');
add_filter('archive_template','job_archive');
//route single- template
function job_single($single_template){
global $post;
$found = locate_template('single-job.php');
if($post->post_type == 'vacancies' && $found != ''){
$single_template = dirname(__FILE__).'/single-job.php';
}
return $single_template;
}
//route archive- template
function job_archive($template){
if(is_post_type_archive('vacancies')){
$theme_files = array('archive-job.php');
$exists_in_theme = locate_template($theme_files, false);
return dirname(__FILE__) . '/archive-job.php';
}
return $template;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment