2016-09-19 53 views
0

我正在使用簡碼獲取頁面內容。短代碼在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"]正常工作的簡碼。任何幫助將是欣賞。

+0

它在阿賈克斯工作得很好。你可以請給我演示網址? –

+0

謝謝穆克什。請查看這兩個網址https://ayanize.com/dev1/(點擊快速查看按鈕)和此網址https://ayanize.com/dev1/product/demo-product/。該男子持有耳朵的圖像是從頁面ID獲取的內容,該頁面ID在點擊快速查看按鈕時不會顯示。 – Ayanize

回答

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

此代碼已經過測試並且工作完美。

+0

謝謝你,但這不是重點。短代碼確實可以提取帖子內容,但是您可以看到這添加在摘錄中。它在單個產品頁面上工作,它在WC簡短說明框中添加,但不在解析Divi簡碼時在ajax彈出窗口 – Ayanize

+0

中調用以獲取代碼在ajax中正常工作的摘錄 –