如何通过Ajax中的短代码获取内容

时间:2016-09-19 07:54:34

标签: jquery ajax wordpress shortcode

我使用短代码来获取页面内容。当在wp编辑器中添加时,短代码运行良好但它似乎不起作用或更好我说它在通过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"]在文本编辑器中添加时可以正常工作,但在Ajax中没有。任何帮助都将是值得赞赏的。

1 个答案:

答案 0 :(得分:0)

对于产品“产品简称”不是产品的内容,如果您想获得该字段,请添加以下代码。

/* 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');

此代码经过测试且运作正常。

相关问题