1
我想檢查任何頁面或帖子的帖子類型,並根據帖子類型顯示一些功能。所以我在functions.php中使用了一個函數,它在管理頁面中使用時返回帖子類型,但不在主題頁面上。如何在functions.php中獲取當前帖子類型以便全局使用?
如果帖子類型爲page
,我需要此函數才能包含php文件。所以它必須在系統範圍內工作(如果可能的話)。在模板中,管理員,編輯和發佈頁面。
下面是我使用的功能:
function vart_current_post_type() {
global $post, $typenow, $current_screen;
if ($post && $post->post_type) {
return $post->post_type;
}
elseif (isset($_GET[ 'post' ])) {
return get_post_type($_GET[ 'post' ]);
}
elseif ($typenow) {
return $typenow;
}
elseif ($current_screen && $current_screen->post_type) {
return $current_screen->post_type;
}
elseif (isset($_REQUEST[ 'post_type' ])) {
return sanitize_key($_REQUEST[ 'post_type' ]);
}
return null;
}
add_action('init', 'vart_current_post_type');
請告訴我,我應該以使其在主題模板和頁面工作?
謝謝。
當您使用page.php文件此方法或index.php的它工作正常,但我需要在functions.php的帖子類型它返回null。 –
你想在「init」鉤子中使用它嗎?它是強制性的嗎? –
是的,當我需要時,我需要獲取帖子類型信息。我不知道是否有可能:) –