2015-09-25 59 views
-1

當我試圖通過這個代碼運行簡碼:Do_shortcode函數在WordPress之外無法工作?

define('WP_USE_THEMES', false);   
require_once('account/wp-blog-header.php'); 
require_once('account/wp-includes/shortcodes.php'); 

global $current_user; 
get_currentuserinfo(); 
print_r($current_user); 

get_header(); 
$page_object = get_page(28); 
var_dump($page_object); 
echo do_shortcode($page_object->post_content); 

我得到頁,只有頭,所以do_shortcode沒有工作。 轉儲page_object的返回我:

object(WP_Post)[276] 
    public 'ID' => int 28 
    public 'post_author' => string '1' (length=1) 
    public 'post_date' => string '2015-09-22 12:14:16' (length=19) 
    public 'post_date_gmt' => string '2015-09-22 12:14:16' (length=19) 
    public 'post_content' => string '[pmpro_levels]' (length=14) 
    public 'post_title' => string 'Membership Levels' (length=17) 
    public 'post_excerpt' => string '' (length=0) 
    public 'post_status' => string 'publish' (length=7) 
    public 'comment_status' => string 'closed' (length=6) 
    public 'ping_status' => string 'closed' (length=6) 
    public 'post_password' => string '' (length=0) 
    public 'post_name' => string 'levels' (length=6) 
    public 'to_ping' => string '' (length=0) 
    public 'pinged' => string '' (length=0) 
    public 'post_modified' => string '2015-09-22 12:14:16' (length=19) 
    public 'post_modified_gmt' => string '2015-09-22 12:14:16' (length=19) 
    public 'post_content_filtered' => string '' (length=0) 
    public 'post_parent' => int 22 
    public 'guid' => string 'http://phpstack-10604-23437-55300.cloudwaysapps.com/account/membership-account-2/membership-levels/' (length=99) 
    public 'menu_order' => int 0 
    public 'post_type' => string 'page' (length=4) 
    public 'post_mime_type' => string '' (length=0) 
    public 'comment_count' => string '0' (length=1) 
    public 'filter' => string 'raw' (length=3) 

而且$page_object->post_content;返回我[pmpro_levels],但 do_shortcode()不解析簡碼。 一切工作在WordPress目錄,你可以看到我打電話給wp-blog-header.php,但它沒有工作。 爲什麼?

回答

0

你應該嘗試apply_filter

$內容= apply_filters( 'the_content',$內容);

apply_filters( 'the_content',$內容);用於將內容過濾器應用於原始未過濾的帖子內容,這通常來自使用$ post-> post_content。這些過濾器包括著名的過濾器wp_autop這增加p標籤到the_content()

apply_filters( 'the_content',$內容);與get_posts其中一個直接與WP_Post對象的工作,而無需使用setup_postdata($後)一起選擇是usally使用,這使得像the_content可使用的模板標籤()

更新 - 這是你的代碼應該如何

get_header(); 
$page_object = get_page(28); 
echo apply_filters('the_content', $page_object->post_content); 
+0

爲什麼OP應該試試?請解釋一下(在答案中)。 –

+0

謝謝你的回覆。 當我嘗試這個 $內容= get_the_content(); \t $ content = apply_filters('the_content',$ content); \t var_dump($ content); 我收到通知:試圖獲取非對象的屬性在/home/31175-23437.cloudwaysapps.com/zzazfhvsnt/public_html/account/wp-includes/post-template.php在線279 – DocNet

+0

請參閱更新的答案 –

相關問題