WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Trouble with getting comment_excerpt() to show (10 posts)

  1. rragle
    Member
    Posted 6 years ago #

    I am trying to use <?php comment_excerpt(); ?> and can't get it or the comment_content tag to work or show up. I have placed it within the loop as the support page says.

    Does anyone know if there is anything I need to include in the header file that will help? Any other ideas?

  2. MichaelH
    Member
    Posted 6 years ago #

    The Template Tag, comment_excerpt, does have to be used in The Loop as you stated, but your question about the 'header file' makes me think you aren't using The Loop.

    What template file are you using for this? (e.g index.php)

    If necessary, please paste the contents of your file at http://wordpress.pastebin.ca/ and report the link back here.

  3. rragle
    Member
    Posted 6 years ago #

    I placed it in index.php. I pasted the loop which includes the comment excerpt tag into the pastebin: http://wordpress.pastebin.ca/738623

  4. MichaelH
    Member
    Posted 6 years ago #

    Hmmm, I'm not sure that does work in a post The Loop.

    Seems like it does work in a comment loop. To see an example, look at wp-content/themes/default/comments.php. If you put <?php comment_excerpt() ?> at line 39 you get a feel for it. Remember it is displaying the first 20 words.

    Note: Of course you then have to click on a post title that has links so it uses the single.php that in turn uses the compents.php template.

  5. MichaelH
    Member
    Posted 6 years ago #

    Striking out the Codex text that says the comment_excerpt must be used in The Loop unless someone wants to explain how to make it work in a post loop.

    See http://codex.wordpress.org/Template_Tags/comment_excerpt

  6. rragle
    Member
    Posted 6 years ago #

    Hmm I wonder if it is possible to fiddle with the comments loop - add it into The Loop?

  7. MichaelH
    Member
    Posted 6 years ago #

    Sure. Just take an example from the wp-content/themes/default/comments.php file where it shows a loop like this:
    <?php foreach ($comments as $comment) : ?>

    or look at wp-admin/edit-comments.php where is says:
    foreach ($comments as $comment) {

  8. rragle
    Member
    Posted 6 years ago #

    I did get it to work actually using this cobbled together from comments-popup.php and comments.php. The part I want to solve now is getting just one to show -preferably the latest. I think it will be hard to show the latest without a custom query directly to the database which I havent tried but if anyone has any ideas that would be great. `<?php if ($comments) : ?>

      <?php

      foreach ($comments as $comment) :
      ?>
      <li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
      <cite><?php comment_author_link() ?></cite> says:
      <?php if ($comment->comment_approved == '0') : ?>
      Your comment is awaiting moderation.
      <?php endif; ?>
      <?php comment_excerpt() ?>

      <?php
      /* Changes every other comment to a different class */
      $oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';
      ?>

      <?php endforeach; /* end for each comment */ ?>

    <?php else : // this is displayed if there are no comments so far ?>

    <?php if ('open' == $post->comment_status) : ?>
    <!-- If comments are open, but there are no comments. -->

    <?php else : // comments are closed ?>
    <!-- If comments are closed. -->
    <p class="nocomments">Comments are closed.</p>

    <?php endif; ?>
    <?php endif; ?>`

  9. MichaelH
    Member
    Posted 6 years ago #

    Aren't they in order with the latest showing first?

    If so, couldn't you just use a counter test, like

    You mean display just the latest comment excerpt?

    Just before the foreach ($comments as $comment) put:
    $commentcount=0

    And then replace php comment_excerpt() with

    $commentcount++;
    if($commentcount==1){
    comment_excerpt()
    }
  10. rragle
    Member
    Posted 6 years ago #

    For some reason they are in chronological order latest at the bottom.

    But your code was very helpful. At least I have only one comment showing. Not the latest - here is the latest code

    <?php if ($comments) : ?>
    
    	<ul>
    <?php $commentcount=0 ?>
    	<?php 
    
    	foreach ($comments as $comment) :
     ?><?php $commentcount++;?>
    <?php if($commentcount==1){ ?>
    		<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
    			<cite><?php comment_author_link() ?></cite> says:
    			<?php if ($comment->comment_approved == '0') : ?>
    			<em>Your comment is awaiting moderation.</em>
    			<?php endif; ?>
    
    <?php comment_excerpt() ?>
    <?php }
    ?>
    		</li>
    
    	<?php
    		/* Changes every other comment to a different class */
    		$oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';
    	?>
    
    	<?php endforeach; /* end for each comment */ ?>
    
    	</ul>
    
     <?php else : // this is displayed if there are no comments so far ?>
    
    	<?php if ('open' == $post->comment_status) : ?>
    		<!-- If comments are open, but there are no comments. -->
    
    	 <?php else : // comments are closed ?>
    		<!-- If comments are closed. -->
    		<p class="nocomments">Comments are closed.</p>
    
    	<?php endif; ?>
    <?php endif; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic