无法获取我的自定义帖子类型名称以加载single-jobs.php页面,有些人可以帮助我在代码中做错的地方,或者是否存在永久链接问题。
function wwp_job_portal_single_page($original_template){
//check post type to job portal single page
$type=get_post_types();
if(get_query_var( 'post_type' ) !== 'jobs'){
return ;
}
elseif(is_single('jobs')){
//check if file exit of single job page template
if(file_exists(file_exists( get_stylesheet_directory(). '/single-jobs.php' ))){
return get_stylesheet_directory() . '/single-jobs.php';
}
else{
return plugin_dir_path( __FILE__ ) . 'templates/single-jobs.php';
}
}
else{
echo "<h1>jobs page loaded</h1>";
return plugin_dir_path( __FILE__ ) . 'templates/single-jobs.php';
}
return $original_template;
}
add_action('template_include','wwp_job_portal_single_page');
function create_jobs_post_type(){
// set up labels
$labels = array(
'name' => 'Job',
'singular_name' => 'Job',
'add_new' => 'Post New Job',
'add_new_item' => 'Post New Job',
'edit_item' => 'Edit Job',
'new_item' => 'New Job',
'all_items' => 'All jobs',
'view_item' => 'View jobs',
'search_items' => 'Search Job',
'not_found' => 'No Job Found',
'not_found_in_trash' => 'No Job found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Jobs',
);
//register post type
register_post_type( 'jobs', array(
'labels' => $labels,
'has_archive' => true,
'public' => true,
'supports' => array('title','thumbnail'),
'publicly_queryable' => true,
'exclude_from_search' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 10,
'menu_icon' => 'dashicons-id-alt',
'can_export' => true,
'delete_with_user' => false,
'hierarchical' => false,
'has_archive' => true,
'query_var' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
// 'capabilities' => array(),
'rewrite' => array(
'slug' => 'jobs',
'with_front' => true,
'pages' => true,
'feeds' => false,
),
)
);
$taxonomy_args = array(
'labels' => array( 'name' => 'Job Category' ),
'show_ui' => true,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'jobs' )
);
register_taxonomy(
'jobs_category',
'jobs',
$taxonomy_args
);
}
//hook for theme setup
add_action('init','create_jobs_post_type');
答案 0 :(得分:1)
函数get_post_type()
可以在循环外使用,但需要设置可选参数($post_id
)。
在您的代码中,您需要将帖子ID设置为参数。
function wwp_job_portal_single_page($original_template){
global $post;
//check post type to job portal single page
$type=get_post_types($post->ID);
.....
然后您的功能将能够找到您要查找的相关帖子ID。
答案 1 :(得分:0)
您可以使用帖子ID尝试相同的功能,就像:
get_post_type( get_the_ID() )
希望能做到