我在Wordpress中使用了一个ajax请求来获取一篇文章的内容,在那篇文章中我使用了Visual Composer。但内容只显示VC的短代码,而没有将其更改为真实内容。。这是我使用的代码
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(); }
这个jQuery代码
$('.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);
}
});
});
但像一样返回
[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]
自版本4.9以来,visualcomposer添加了短代码延迟加载。要在AJAX内容上使用VC短代码,请在打印内容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(); }
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.
}
这就是你想要做的。我个人不知道为什么,但只要你在启动调用之前使用方法和类检查,它就会起作用。我的猜测可能与ajax回调之前运行的过滤器/钩子有关。
此外,需要注意的是,如果您通过ajax在主查询之外运行循环,您希望在每次$query->the_post()之后运行此循环,因为这会为函数(如_title())设置上下文;
要扩展上述解决方案,如果您使用的帖子有自定义css样式的更改,如边框、背景等。您还需要使用get_post_meta($pid, '_wpb_shortcodes_custom_css', true);
获得自定义css
示例:
$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;