2016-09-24 41 views
0

我一直在尝试过去几个小时来研究如何从wordpress获得最新评论。这里是我如何设法获取最近的帖子.... wordpress得到最近的评论

<h4>Recent Posts</h4> 
    <ul> 
     <?php 
      $args = array('numberposts' => '5'); 
      $recent_posts = wp_get_recent_posts($args); 
      foreach($recent_posts as $recent){ 
       echo '<li><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a> </li> '; 
      } 
      wp_reset_query(); 
     ?> 
    </ul> 

我如何获得最新评论..

PS。我试图将帖子更改为评论,并且不起作用。

在此先感谢

史蒂芬

+0

您可以使用一个小部件(有一个默认情况下,WordPress的):http://www.wpbeginner.com/beginners-guide/how-to-show -recent-comments-in-wordpress-sidebar/ – mimarcel

+0

我知道我可以,但我宁愿学习代码...谢谢。 – scottiescotsman

回答

0

可以使用get_comments()检索最近的评论。

get_comments()以与您用于检索帖子的功能非常类似的方式工作。

<?php $recent_comments = get_comments(array( 
    'number'  => 5, // number of comments to retrieve. 
    'status'  => 'approve', // we only want approved comments. 
    'post_status' => 'publish' // limit to published comments. 
)); 

if ($recent_comments) { 
    foreach ((array) $recent_comments as $comment) { 

     // sample output - do something useful here 
     echo '<a href="' . esc_url(get_comment_link($comment)) . '">' . get_the_title($comment->comment_post_ID) . '</a>'; 

    } 
} 

延伸阅读:https://codex.wordpress.org/Function_Reference/get_comments