• Resolved AWKampen

    (@awkampen)


    Hi,

    I am trying to make a custum ‘Recent Comments’ code for use in the sidebar. I have come a long way, but there is just one thing that doesn’t do what I’d like it to do.

    What I want is that, per page with comments that is found, only the last comment is shown. I was hoping post_per_page would do the trick, but it doesn’t!

    This is my code:

    $args=array(
    	'author_email' => '',
    	'ID' => '',
    	'karma' => '',
    	'number' => 5,
    	'offset' => '',
    	'orderby' => 'comment_date_gmt',
    	'order' => 'DESC',
    	'parent' => '',
    	'post_id' => '',
    	'post_author' => '',
    	'post_name' => '',
    	'post_parent' => '0',
    	'post_status' => 'publish',
    	'post_type' => 'page',
    	'posts_per_page' => 1,
    	'status' => 'approve',
    	'type' => '',
    	'user_id' => '',
    	'search' => '',
    	'count' => false
    );
    $comments = get_comments($args);
    
    foreach($comments as $comment) :
    
    ?>
    <div style="border: 0px solid #000000; height: 1%; overflow: hidden;">
    <div style="border: 0px solid #000000; float: left; width: 28%;">
    <a href="<?php echo get_author_posts_url($comment->user_id); ?>"><?php echo get_avatar( $comment, 48 ); ?></a>
    </div>
    <div style="border: 0px solid #000000; float: right; width: 72%;">
    <?php
    $aantalcomments = get_comments_number( $comment->comment_post_ID );
    
    ?>
    	<a href="<?php echo get_author_posts_url($comment->user_id); ?>"><?php echo($comment->comment_author) ?></a> op:<br>
    
    	<a href="<?php echo get_comment_link($comment->comment_ID); ?>">
    
    <?php
    	echo($comment->post_name . " (" . $aantalcomments . ")<br>");
    ?>
    </a>
    <?php
    
    echo get_comment_date(' l, j F, Y ', $comment->comment_ID);
    
    ?>
    </div>
    </div>
    <hr>
    <?php
    
    endforeach;
Viewing 2 replies - 1 through 2 (of 2 total)
  • Use a PHP break as the last statement inside the foreach loop. That will exit the loop after the first pass.

    Thread Starter AWKampen

    (@awkampen)

    Well, I couldn’t figure it out, but my brother, who is REALLY GOOD at PHP did figure it out. He himself doesn’t consider this the best of solution, but hey, it works. Maybe a quick-fix of which he isn’t proud, but I am saved! ?? He was even so kind as to comment his work. I hope someone else can use this as well!

    <?php
    
    $args=array(
        'author_email' => '',
        'ID' => '',
        'karma' => '',
        'number' => 5,
        'offset' => '',
        'orderby' => 'comment_date_gmt',
        'order' => 'DESC',
        'parent' => '',
        'post_id' => '',
        'post_author' => '',
        'post_name' => '',
        'post_parent' => '0',
        'post_status' => 'publish',
        'post_type' => 'page',
        'posts_per_page' => 1,
        'status' => 'approve',
        'type' => '',
        'user_id' => '',
        'search' => '',
        'count' => false,
        'group' => 'post_id'
    );
    $comments = get_comments($args);
    
    // Due to wordpress not having a common functionality, we create a new list of comments manually
    $filteredComments = array();
    // Posts we already have a comment for
    $filteredUsedPosts = array();
    // Amount of comments
    $filteredCommentLimit = 5;
    
    // Loop through all comments
    foreach($comments as $comment) {
        // Have we reached our amount of coments, then we?e finished
        if (count($filteredComments) < $filteredCommentLimit){
            // Do we already have comment for this post?
            if (!in_array($comment->comment_post_ID, $filteredUsedPosts)){
                // We add the POST ID to the list of used POST IDs
                $filteredUsedPosts[] = $comment->comment_post_ID;
                // We add our comment to the display comments list
                $filteredComments[] = $comment;
            }
        }
    }
    
    foreach($filteredComments as $comment) :
    
        ?>
    <div style="border: 0px solid #000000; height: 1%; overflow: hidden;">
        <div style="border: 0px solid #000000; float: left; width: 28%;">
            <a href="<?php echo get_author_posts_url($comment->user_id); ?>"><?php echo get_avatar( $comment, 48 ); ?></a>
        </div>
        <div style="border: 0px solid #000000; float: right; width: 72%;">
            <?php
            $aantalcomments = get_comments_number( $comment->comment_post_ID );
    
            ?>
            <a href="<?php echo get_author_posts_url($comment->user_id); ?>"><?php echo($comment->comment_author) ?></a> op:<br>
    
            <a href="<?php echo get_comment_link($comment->comment_ID); ?>">
    
                <?php
                echo($comment->post_name . " (" . $aantalcomments . ")<br>");
                ?>
            </a>
            <?php
    
            echo get_comment_date(' l, j F, Y ', $comment->comment_ID);
    
            ?>
        </div>
    </div>
    <hr>
    <?php
    
    endforeach;
    
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘One comment per page in foreach loop’ is closed to new replies.