dongshan4316 2013-07-08 20:04
浏览 7

在Wordpress中输出最近的帖子和摘录,而不是当前页面

I am trying to output recent posts plus the excerpt onto my homepage using the following code:

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

This seems to output the title and the permalink just fine, however it does not output the excerpt.

Hope someone can help

  • 写回答

2条回答 默认 最新

  • dr9379 2013-07-08 20:13
    关注

    try this one

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

    Make sure your post_excerpt is not empty

    If you want to add the post_excerpt then use 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 );
    

    As per your request in comments i am showing you the demo to update the post by copying the post_title in the post_excerpt so here you go

    <?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

    Also see wp_insert_post

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向