如何在Ajax中通过简码获取内容
问题描述:
我正在使用简码获取页面内容。短代码在wp编辑器中添加时运行良好,但它似乎不工作或更好,我说它通过ajax时不解析。如何在Ajax中通过简码获取内容
我在网站上显示通过Ajax显示WooCommerce产品信息的弹出窗口。简码仅显示原始码,不会被解析。这是简码
function fetch_content_shortcode($atts, $content = null)
{
global $post;
extract(shortcode_atts(array(
'id' => null
), $atts));
ob_start();
$output = apply_filters('the_content', get_post_field('post_content', $id));
$output .= ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode('fetch-content', 'fetch_content_shortcode');
在文本编辑器,但不与阿贾克斯添加时[fetch-content id="1234"]
正常工作的简码。任何帮助将是欣赏。
答
对于产品“产品简介”不是产品的内容,如果你想获得该领域,然后添加下面的代码。
/* If you want content of page, post or product */
$output = apply_filters('the_content', get_post_field('post_content', $id));
/* If you want excerpt of page, post or product */
$output = apply_filters('the_excerpt', get_post_field('post_excerpt', $id));
Shortcode to get product excerpt:
function fetch_content_shortcode($atts, $content = null) {
global $post;
extract(shortcode_atts(array(
'id' => null
), $atts));
ob_start();
/* If you want excerpt of page, post or product */
$output = apply_filters('the_excerpt', get_post_field('post_excerpt', $id));
$output .= ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode('fetch-content', 'fetch_content_shortcode');
此代码已经过测试并且工作完美。
+0
谢谢你,但这不是重点。短代码确实可以提取帖子内容,但是您可以看到这添加在摘录中。它在单个产品页面上工作,它在WC简短说明框中添加,但不在解析Divi简码时在ajax弹出窗口 – Ayanize
+0
中调用以获取代码在ajax中正常工作的摘录 –
它在阿贾克斯工作得很好。你可以请给我演示网址? –
谢谢穆克什。请查看这两个网址https://ayanize.com/dev1/(点击快速查看按钮)和此网址https://ayanize.com/dev1/product/demo-product/。该男子持有耳朵的图像是从页面ID获取的内容,该页面ID在点击快速查看按钮时不会显示。 – Ayanize