【问题标题】:Do_shortcode function does not work outside WordPress?Do_shortcode 函数在 WordPress 之外不起作用?
【发布时间】:2015-09-25 19:52:09
【问题描述】:

当我尝试通过此代码运行短代码时:

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 的地方看到,但它不起作用。 为什么?

【问题讨论】:

    标签: php wordpress shortcode


    【解决方案1】:

    你应该试试 apply_filter

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

    apply_filters('the_content', $content);用于将内容过滤器应用于原始未过滤的帖子内容,这通常来自使用 $post->post_content。这些过滤器包括著名的过滤器 wp_autop,它将 p 标签添加到 the_content()

    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);
    

    【讨论】:

    • 为什么 OP 应该尝试一下?请解释一下(在答案内)。
    • 感谢您的回复。当我尝试这个 $content = get_the_content(); $content = apply_filters('the_content', $content); var_dump($内容);我收到通知:尝试在第 279 行的 /home/31175-23437.cloudwaysapps.com/zzazfhvsnt/public_html/account/wp-includes/post-template.php 中获取非对象的属性
    • @LahiruNanayakkara 不,回声的结果只是: [pmpro_levels] 。 :/ 'the_content' 是什么意思?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多