问题
I used an ajax request in Wordpress to get the content of a post, in that post i used the Visual Composer. but the content shows only the VC shortcodes with out changing them to the real content.. That is the code that i used
add_action( 'wp_ajax_drpk_custom','drpk_custom' );
add_action( 'wp_ajax_nopriv_drpk_custom','drpk_custom' );
function drpk_custom(){
if(isset($_REQUEST)){
$id = $_REQUEST['id'];
$query = new WP_Query(array('p'=>$id));
if($query->have_posts()):
while($query->have_posts()): $query->the_post();?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel"><?php the_title() ?></h4>
</div>
<div class="modal-body">
<?php the_content() ?>
</div>
<?php endwhile;
endif;
}
wp_die(); }
And this jQuery code
$('.cart-item').find('a').on('click',function(){
var postID = $(this).data('id'),
ajaxContent = $('.modal-content');
$.ajax({
url: ajaxUrl.url,
data: {
'action' : 'drpk_custom',
'id': postID
},
beforeSend: function(){
// $load.fadeIn(500);
},
success: function(data){
// $load.hide();
ajaxContent.html(data);
}
});
});
but returns like that
[vc_row][vc_column width=”1/4″][vc_single_image image=”389″ img_size=”full” alignment=”center” onclick=”custom_link” img_link_target=”_blank” link=”#”][/vc_column][vc_column width=”1/4″][vc_single_image image=”390″ img_size=”full” alignment=”center” onclick=”custom_link” img_link_target=”_blank” link=”#”][/vc_column][vc_column width=”1/4″][vc_single_image image=”391″ img_size=”full” alignment=”center” onclick=”custom_link” img_link_target=”_blank” link=”#”][/vc_column][vc_column width=”1/4″][vc_single_image image=”392″ img_size=”full” alignment=”center” onclick=”custom_link” img_link_target=”_blank” link=”#”][/vc_column][/vc_row]
回答1:
Since version 4.9 visual composer added shortcode lazy loading. To use VC shortcodes on AJAX content use this function before printing the content WPBMap::addAllMappedShortcodes();
add_action( 'wp_ajax_drpk_custom','drpk_custom' );
add_action( 'wp_ajax_nopriv_drpk_custom','drpk_custom' );
function drpk_custom(){
if(isset($_REQUEST)){
$id = $_REQUEST['id'];
$query = new WP_Query(array('p'=>$id));
if($query->have_posts()):
while($query->have_posts()): $query->the_post();
WPBMap::addAllMappedShortcodes();
?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel"><?php the_title() ?></h4>
</div>
<div class="modal-body">
<?php the_content() ?>
</div>
<?php endwhile;
endif;
}
wp_die(); }
回答2:
if(class_exists('WPBMap') && method_exists('WPBMap', 'addAllMappedShortcodes')) { // 1.17c. FIxes issues with ajax hopefully.
WPBMap::addAllMappedShortcodes(); // this is needed to ensure wc shortocdes come out right.
}
This would be what you'd want to do. I don't know why personally, but it works so long as you use a method and class check prior to initiating the call.My guess would be something to do with the filters/hooks run prior to the ajax callback.
Additionally, it should be noted that if you're running a loop outside the main query via ajax, you want to run this after every $query->the_post() as this sets up context for functions like the_title();
回答3:
To expand on above solutions, if the post you're using has custom css style changes like border, background, etc.. you'll need to get that custom css too using get_post_meta($pid, '_wpb_shortcodes_custom_css', true);
Example:
$thepost = get_post($pid);
WPBMap::addAllMappedShortcodes();
$content = $thepost->post_content;
$content = apply_filters('the_content', $content);
$vccss = get_post_meta($pid, '_wpb_shortcodes_custom_css', true);
if(!empty($vccss))
{
$vccss = strip_tags($vccss);
$content.='<style type="text/css" data-type="vc_shortcodes-custom-css">';
$content.=$vccss;
$content.='</style>';
}
echo $content;
来源:https://stackoverflow.com/questions/37007573/how-to-load-visual-composer-during-using-ajax-in-wordpress