2016-09-24 5 views
0

私は最後の数時間のwordpressから最近のコメントを取得する方法を研究するために努力してきました。ここで私はwordpress最近のコメントを取得

PS ..私は最新のコメントを得るのですか

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

....最近の投稿を得ることができた方法です。投稿をコメントに変更しようとしましたが、うまくいきません。

事前に感謝

スティーブン

+0

は(ワードプレスのデフォルトずつあります):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