douzhi7070 2018-02-15 12:38
浏览 60

使用AJAX加载wordpress页面并解析主题短代码

I am currently creating a wordpress page based on the X Theme which utilises Cornerstone as a page builder.

I am trying to dynamically load pages into the main page with AJAX. For this I created a script making a request to admin-ajax.php which works flawlessly.

Now the tricky part: I added a PHP snippet with the plugin Code Snippets containing the following code:

function get_case() {
    global $wpdb;

    $postNo = intval( $_POST['case'] );

    $post = get_post($postID);
    $content = apply_filters('the_content', $post->post_content);
    $html = do_shortcode($content)
    echo $html;

    wp_die();
}

add_action( 'wp_ajax_get_case', 'get_case' );
add_action( 'wp_ajax_nopriv_get_case', 'get_case' );

This code returns correct results if I am using the wordpress page builder, however it simply returns the shortcodes of the theme if the page was built in Cornerstone.

e.g.

[cs_element_section _id=”1″][cs_element_row _id=”2″]...

Am I doing this correctly? Shouldn't do_shortcodes() resolve those shortcodes? Is there any other way to dynamically load sub pages?

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • doudao0660 2018-02-15 13:02
    关注

    You actually don't need do_shortcode (or global $wpdb if this is your full code) because that is run automatically on the_content assuming it wasn't unhooked elsewhere. We can't see your full set-up (plugins, theme, custom code) to fully debug, but this code below is all you would need on a Plain Vanilla WordPress site to accomplish what you're trying to do. If this doesn't work, the problem is elsewhere in your set-up:

    function get_case() {
      $postNo = intval( $_POST['case'] );
      $post = get_post($postNo); // NOTE: Your code has the wrong variable here
      $content = apply_filters('the_content', $post->post_content);
      exit( $content );
    }
    add_action( 'wp_ajax_get_case', 'get_case' );
    add_action( 'wp_ajax_nopriv_get_case', 'get_case' );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分