無法獲取我的自定義帖子類型名稱以加載single-jobs.php頁面有些人可以幫助我,我在代碼中做錯了什麼或者有沒有永久鏈接問題。就業張貼型無法使用get_post_type()獲取自定義帖子類型名稱
代碼加載一個頁面模板
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');
謹慎的話,使用相同的'slug'的職位類型和稅收可能會導致問題。改變它,看看你是否有任何改善。 另外,你的single-jobs.php看起來如何。檔案如何尋找這個問題。什麼是邏輯 – ste
改變稅的slu but,但仍然無法讓我的自定義職位類型名稱加載模板單 - { - }。php文件。讓我再次解釋我面臨的問題,我想要檢測我的自定義帖子類型以加載我的單個{ - } .php頁面文件模板包含鉤子。如我的代碼所示。你有沒有看我的帖子註冊碼,你可以指導我嗎?有什麼我錯過了,導致整個問題。感謝x回覆。 – user2651865