【问题标题】:Load CF7 Form via Ajax通过 Ajax 加载 CF7 表单
【发布时间】:2013-05-11 17:50:37
【问题描述】:

我将通过 ajax 加载页面内容,这些内容包含在我通过 CF7 插件生成的表单之一的简码内。 呈现内容时,不处理短代码并以文本形式打印。 无论如何在ajax调用中强制执行短代码? 谢谢, M.

这是js脚本:

function getcontent(postid){
    // here is where the request will happen
    jQuery.ajax({
        url: '/wp-admin/admin-ajax.php',//ajax controller request
        data:{
            'action':'dch',//action invoked
            'fn':'ghcontent',//function required
            'postid':postid//if of post required
        },
        cache: false,
        async:true,
        timeout: 3000,
        success:function(data){
            jQuery("#posthome").html(data);//print the html (content)
        },
        error: function(errorThrown){
            console.log(errorThrown);
        }
    });
}

这是我的 php 代码:

add_action('wp_ajax_nopriv_dch', 'ghcontent');
add_action('wp_ajax_dch', 'ghcontent');
function ghcontent(){
  $args = array('page_id' => $_REQUEST['postid']);
  query_posts($args);
  if(have_posts()) : 
    while (have_posts()) : the_post();
      the_content();
    endwhile;
  endif;
  die();
}

【问题讨论】:

    标签: php jquery wordpress contact-form-7


    【解决方案1】:

    do_shortcode 不能在您的 admin-ajax.php 中工作,而是尝试生成您自己的 ajax 操作。检查以下类似问题的答案: https://wordpress.stackexchange.com/questions/53309/why-might-a-plugins-do-shortcode-not-work-in-an-ajax-request

    【讨论】:

      【解决方案2】:

      the_content 过滤器运行时应用短代码:

      $post = get_post( $_REQUEST['postid'] );
      $return = apply_filters( 'the_content', $post->post_content );
      echo $return;
      die();
      

      【讨论】:

      • 我已经尝试过使用 the_content 和 apply_filters 函数,但不起作用!有什么建议吗?谢谢:)
      • 有点不合常规,但你可以试试do_shortcode( get_the_content() ) 之类的东西
      • 我已经用我的代码更新了问题,也许是 ajax 调用错误?非常感谢!
      • 我会使用get_post 来获取帖子数据,更新答案,希望对您有所帮助!
      • 好吧,也许问题是因为我在主页中通过 ajax 调用内容?有解决办法吗?谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      相关资源
      最近更新 更多