0

我正在尝试使用以下代码将最近的帖子和摘录输出到我的主页上:

<?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> ';
    }
?>

这似乎可以很好地输出标题和永久链接,但是它不输出摘录。

希望有人可以帮助

4

2 回答 2

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
于 2013-07-09T05:32:25.467 回答
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> ';
    }
?>

wp_update_post

另见wp_insert_post

于 2013-07-08T20:13:57.253 回答