Php 在wordpress中使用ajax时如何加载visual composer
我在Wordpress中使用了一个ajax请求来获取一篇文章的内容,在那篇文章中我使用了VisualComposer。但内容仅显示VC短代码,没有将其更改为真实内容。。这就是我使用的代码Php 在wordpress中使用ajax时如何加载visual composer,php,jquery,ajax,wordpress,visual-composer,Php,Jquery,Ajax,Wordpress,Visual Composer,我在Wordpress中使用了一个ajax请求来获取一篇文章的内容,在那篇文章中我使用了VisualComposer。但内容仅显示VC短代码,没有将其更改为真实内容。。这就是我使用的代码 add_action( 'wp_ajax_drpk_custom','drpk_custom' ); add_action( 'wp_ajax_nopriv_drpk_custom','drpk_custom' ); function drpk_custom(){ if(isset($_REQUEST)){
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(); }
但结果是这样的
[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');
添加动作('wp\u ajax\u nopriv\u drpk\u custom','drpk\u custom');
函数drpk_custom(){
如果(isset($\请求)){
$id=$\u请求['id'];
$query=新的WP_查询(数组('p'=>$id));
如果($query->have_posts()):
而($query->have_posts()):$query->the_post();
WPBMap::addAllMappedShortcodes();
?>
&时代;
从版本4.9开始,visual composer添加了短代码延迟加载。要在AJAX内容上使用VC短代码,请在打印内容之前使用此函数WPBMap::addAllMappedShortcodes();
add_action('wp_ajax_drpk_custom','drpk_custom');
添加动作('wp\u ajax\u nopriv\u drpk\u custom','drpk\u custom');
函数drpk_custom(){
如果(isset($\请求)){
$id=$\u请求['id'];
$query=新的WP_查询(数组('p'=>$id));
如果($query->have_posts()):
而($query->have_posts()):$query->the_post();
WPBMap::addAllMappedShortcodes();
?>
&时代;
这就是您想要做的。我个人不知道为什么,但只要您在启动调用之前使用方法和类检查,它就可以工作。我猜可能与ajax回调之前运行的过滤器/挂钩有关
此外,应该注意的是,如果您通过ajax在主查询之外运行循环,那么您希望在每个$query->the_post()之后运行该循环,因为这将为诸如_title()之类的函数设置上下文
这就是您想要做的。我个人不知道为什么,但只要您在启动调用之前使用方法和类检查,它就可以工作。我猜可能与ajax回调之前运行的过滤器/挂钩有关
此外,应该注意的是,如果您通过ajax在主查询之外运行循环,那么您希望在每个$query->the_post()之后运行该循环,因为这将为诸如_title()之类的函数设置上下文要扩展上述解决方案,如果您正在使用的帖子有自定义css样式的更改,如边框、背景等。您也需要使用get_post_meta($pid,'.\u wpb_shortcode_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;
$thepost=get\u post($pid);
WPBMap::addAllMappedShortcodes();
$content=$thepost->post\u内容;
$content=apply_过滤器('the_content',$content);
$vccss=get_post_meta($pid,'.'wpb'.'u快捷码'.'u自定义'u css',true);
如果(!empty($vccss))
{
$vccss=带标签($vccss);
$content.='';
$content.=$vccss;
$content.='';
}
echo$内容;
要扩展上述解决方案,如果您正在使用的帖子有自定义css样式的更改,如边框、背景等。您也需要使用get_post_meta($pid,'.\u wpb_shortcode_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;
$thepost=get\u post($pid);
WPBMap::addAllMappedShortcodes();
$content=$thepost->post\u内容;
$content=apply_过滤器('the_content',$content);
$vccss=get_post_meta($pid,'.'wpb'.'u快捷码'.'u自定义'u css',true);
如果(!empty($vccss))
{
$vccss=带标签($vccss);
$content.='';
$content.=$vccss;
$content.='';
}
echo$内容;
在之前调用WPBMap::addAllMappedShortcodes()
内容()在AJAX主题中对我不起作用,浏览器控制台中也不会显示错误。在之前调用WPBMap::addAllMappedShortcodes()
内容()在AJAX主题中对我不起作用,浏览器控制台中也不会显示错误。
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.
}
$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;