2017-07-18 67 views
0

我試圖做一個簡碼來輸出特定的頁面摘錄。研究,但沒有輸出結構的代碼。通過頁面ID獲取頁面摘錄wordpress

function page_shortcode($atts) 
    { 
     $page_id = 173; 
     $page_data = get_page($page_id); 
     $the_excerpt = $page_data->post_excerpt; 


     echo $the_excerpt; 

    } 

    add_shortcode('page_shortcode_excerpt', 'page_shortcode'); 

回答

0

頁摘錄激活碼是好的!但您在簡碼中使用的get_page函數已被棄用。你可以在這裏查詢enter link description here瞭解更多信息。

嘗試把下面的代碼爲您的簡碼:

function page_shortcode($atts) 
{ 
    $page_id = 173; 
    $page_data = get_post($page_id); 
    $the_excerpt = $page_data->post_excerpt; 

    return $the_excerpt; 
} 
add_shortcode('page_shortcode_excerpt', 'page_shortcode'); 

此外,當你從你的管理面板編輯與ID = 173頁,從屏幕選項在頁面的頂部檢查摘錄複選框,並檢查摘錄字段有或沒有任何值。如果不在前端摘錄將不會顯示任何內容。

希望這會幫助你!

0

我只是需要激活頁面上摘錄使用此行代碼

add_action('init', 'add_excerpts_to_pages'); 
function add_excerpts_to_pages() { 
    add_post_type_support('page', 'excerpt'); 
} 

參考:excerpt on page