2013-07-08 59 views
0
當前頁面

我想要輸出最近的帖子加上使用下面的代碼摘錄到我的主頁:輸出最近的職位,摘錄在WordPress的,不是

<?php 
     $args = array('numberposts' => '3'); 
    $recent_posts = wp_get_recent_posts($args); 
    foreach($recent_posts as $recent){ 
     echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.$recent["post_title"].'" >' . $recent["post_title"].'</a>' . $recent["post_excerpt"] . ' </li> '; 
    } 
?> 

這似乎輸出標題和固定鏈接只是很好,但是它不輸出摘錄。

希望有人能幫助

回答

0

試試這個

<?php 
     $args = array('post_type'=>'post', 
'orderby'=>'post_date', 
'post_status'=>'publish', 
'order'   => 'DESC', 
'showposts' => '3'); 
    $recent_posts = get_posts($args); 
    foreach($recent_posts as $recent){ 
     echo '<li><a href="' . get_permalink($recent->ID) . '" title="Look '.$recent->post_title.'" >' . $recent->post_title.'</a>' . $recent->post_excerpt . ' </li> '; 
    } 
?> 

確保您post_excerpt不是空

如果您要添加的post_excerpt然後使用wp_update_post

$my_post = array(); 
    $my_post['ID'] = 37;// it is important 
    $my_post['post_excerpt'] = 'This is the updated post excerpt.'; 


    wp_update_post($my_post); 

根據你的要求評論我顯示你的演示用所以這裏複製在post_excerptpost_title更新post你去

<?php 
     $args = array('post_type'=>'post', 
'orderby'=>'post_date', 
'post_status'=>'publish', 
'order'   => 'DESC', 
'showposts' => '3'); 
    $recent_posts = get_posts($args); 

    foreach($recent_posts as $recent){ // this foreach to add the excerpt 
      $my_post = array(); 
    $my_post['ID'] = $recent->ID;// it is important 
    $my_post['post_excerpt'] = $recent->post_content;  
    wp_update_post($my_post); 
    } 

    foreach($recent_posts as $recent){ // this foreach to show the excerpt 
     echo '<li><a href="' . get_permalink($recent->ID) . '" title="Look '.$recent->post_title.'" >' . $recent->post_title.'</a>' . $recent->post_excerpt . ' </li> '; 
    } 
?> 

wp_update_post

另見wp_insert_post

+0

這仍然不會輸出任何摘錄。你能向我解釋如何使摘錄不是「空的」?我認爲節錄內容截短。 – Francesca

+0

var_dump($ recent_posts);看看裏面有什麼 –

+0

@Francesca查看我的更新回答 –

2

把數組中所需自定義後像這在你的functions.php

$args = array(
     'supports' => array('title','editor','author','excerpt') // by writing these lines an custom field has been added to CMS 
); 

用於在前端檢索

echo $post->post_excerpt; // this will return you the excerpt of the current post 
相關問題