2016-02-12 113 views
0

我想要get_post_type當前帖子。get_post_type()函數不返回任何東西

如果我嘗試get_post_type(),它不會返回任何東西。

我試過get_post_type(124)它工作。 124是當前的post_id。

在我的孩子主題function.php

<?php 
global $post; 
$my_post_type = get_post_type($post->ID); 
?> 

可有人告訴我如何使get_post_type()工作?

回答

0

試試這個(必須是內環路)

get_post_type(get_the_ID()) 

外循環:

global $post; 
get_post_type($post->ID) 
+0

我不放在循環內。我只是把它們放在了兒童主題的function.php中。 – John

+0

第二個選項應該適合你 – designtocode

+0

我得到了錯誤:注意:試圖獲取非對象的屬性在C:\ xampp \ htdocs \ wp-content \ themes \ ma-child \ functions.php 92行 – John

0

你們是不是要在一個循環中做到這一點?如果是這樣,你可以嘗試:

$current_post_type = get_post_type($post->ID); 
echo $current_post_type; 
+0

我不要放在循環中,我只是把它們放在child.php中。 – John

相關問題