简体   繁体   English

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

[英]Output recent posts and excerpt in Wordpress, not of current page

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 希望有人能提供帮助

put the array in your desired custom post like this in your functions.php 在您的functions.php中将数组放入所需的自定义帖子中

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

For retrieving at front end 用于前端检索

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

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 确保您的post_excerpt不为空

If you want to add the post_excerpt then use wp_update_post 如果要添加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 );

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 根据您在评论中的请求,我向您展示了通过复制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_update_post

Also see wp_insert_post 另见wp_insert_post

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM