簡體   English   中英

Do_shortcode 功能在 WordPress 之外不起作用?

[英]Do_shortcode function does not work outside 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 的地方所看到的,但它不起作用。 為什么?

你應該試試 apply_filter

$content = apply_filters('the_content', $content);

apply_filters('the_content', $content); 用於將內容過濾器應用於未經過濾的原始帖子內容,這通常來自 $post->post_content 的使用。 這些過濾器包括著名的過濾器 wp_autop,它向 the_content() 添加了 p 個標簽

apply_filters('the_content', $content); 通常與 get_posts 結合使用,其中直接使用 WP_Post 對象而不使用 setup_postdata( $post ) 這使得像 the_content() 這樣的模板標簽可用

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM