• Resolved denzel2364

    (@denzel2364)


    Hi there,

    I have this piece of code to show the comments on the front page (static)

    <?php $comments = get_comments('status=approve&number=5'); foreach($comments as $comm) :?>
    <a href="<?php echo get_permalink($comm->comment_post_ID);?>#comment-<?php comment_ID() ?>"><?php echo ($comm->comment_content);?></a>
      <?php endforeach;?>

    However, it displays the entire comment. What can i do to make is shorter?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter denzel2364

    (@denzel2364)

    i have this code to make the titles shorter:

    <?php
    	function short_title($after = '', $length) {
    	$mytitle = get_the_title();
    	if ( strlen($mytitle) > $length ) {
    	$mytitle = substr($mytitle,0,$length);
    	echo $mytitle . $after;
    	} else {
    	echo $mytitle;
    	}
    }
    ?>

    [moderated–bump removed. Please refrain from bumping as per Forum Rules]

    you could combine the codes that you have:

    <?php $comments = get_comments('status=approve&number=5'); foreach($comments as $comm) :?>
    <?php
    	$length = 40; // adjust to needed length
    	$thiscomment = $comm->comment_content;
    	if ( strlen($thiscomment) > $length ) {
    	$thiscomment = substr($thiscomment,0,$length);
    	$thiscomment = $thiscomment .' ...';
    	}
    ?>
    <a href="<?php echo get_permalink($comm->comment_post_ID);?>#comment-<?php comment_ID() ?>"><?php echo ($thiscomment);?></a><br />
      <?php endforeach;?>

    good luck ??

    Thread Starter denzel2364

    (@denzel2364)

    oh my god!! You are a legend

    That looks like that had worked.

    Lastly i need it to target a specific category. what would i need to do?

    i am no expert in php and query, however i put this code together which could probably be turned into a function (used in category.php):

    <?php
    //get the cat id of this category,
    //or alternatively set it to your specific cat
    	$cat_id = get_query_var('cat');
    //get all posts with this cat
    	$postwithcats = query_posts('cat='.$cat_id.'&posts_per_page=-1');
    	$i = 0;
    //go through all thesse posts
    foreach ($postwithcats as $postwithcat) :
    	$postid=$postwithcat->ID;
    //get all comments for each post
    	$comments=get_comments('post_id='.$postid);
    
    	 if($comments):
    //get the text of each comment and save it in array element
    	foreach ($comments as $comm) :
    	$thiscomment[$i] = $comm->comment_content;
    //instead of comment content you could save date or author,
    //or save these data in another array
    
    	$i++;
    	endforeach;
    	endif;
    endforeach;
    //all comment texts of this specific cat are now in this array
    
    //if it is needed and comments exist, echo each of them as output
    if($thiscomment):
       foreach ($thiscomment as $key => $val) {
    //before or instead of echo you could perform other actions,
    //such as sort the array elements, shorten the output
    //but in this example simply
    //output the whole comment text of each comment for each post of the category
       echo $val;
    //add space between comment output
       echo '<br /><br />';
       }
    endif;
    ?>

    i briefly tried the code, but it is not extensively tested ??

    Thread Starter denzel2364

    (@denzel2364)

    Looks good, thank you. What would this do? Have you an example?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Limit the amount of characters displayed in comments’ is closed to new replies.