我正在尝试使用以下代码将最近的帖子和摘录输出到我的主页:
<?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 :(得分: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
答案 1 :(得分: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
中的post_title
更新post_excerpt
的演示,以便您在这里
<?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> ';
}
?>