【问题标题】:How to fetch content via shortcode in Ajax如何通过 Ajax 中的简码获取内容
【发布时间】:2016-09-19 07:54:34
【问题描述】:

我正在使用简码来获取页面内容。短代码在 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 中使用。任何帮助将不胜感激。

【问题讨论】:

  • 在 ajax 中运行良好。你能把演示网址发给我吗?
  • 谢谢穆克什。请查看这两个 URL ayanize.com/dev1(单击快速查看按钮)和此 URL ayanize.com/dev1/product/demo-product。拿着耳朵的人的图像是从页面 ID 中获取的内容,当单击“快速查看”按钮时,该 ID 不显示。

标签: jquery ajax wordpress shortcode


【解决方案1】:

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

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

此代码已经过测试并且运行良好。

【讨论】:

  • 谢谢你,但这不是重点。短代码确实在获取帖子内容,但正如您所见,这是在摘录中添加的。它在单个产品页面上工作,它被添加到 WC 简短描述框中,但在 ajax 弹出窗口中调用 Divi 短代码时不解析它们
  • 摘录代码在 ajax 中也能正常工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-22
相关资源
最近更新 更多