0

i would load via ajax the content of page that have inside the shortcode of one of my form generated via CF7 Plugin. When the content is rendered the shortcode is not processed and come print as text. Is there anyway to force execution of shortcode in an ajax call? Thank you, M.

This is the js script:

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

And this is my php code:

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();
}
4

2 回答 2

0

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

于 2015-01-12T17:30:20.810 回答
0

过滤器运行时应用简码the_content

$post = get_post( $_REQUEST['postid'] );
$return = apply_filters( 'the_content', $post->post_content );
echo $return;
die();
于 2013-05-11T18:02:59.173 回答