簡體   English   中英

在Wordpress中輸出最近的帖子和摘錄,而不是當前頁面

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

我正在嘗試使用以下代碼將最近的帖子和摘錄輸出到我的主頁:

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

這似乎輸出標題和永久鏈接就好了,但它不輸出摘錄。

希望有人能提供幫助

在您的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

試試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM