• I’m somewhat new to wordpress, and I’ve been trying to get a hang of how it all works. I’ve managed to set up the “loop” in my main PHP file (I’m not using themes) and now I’d like to start working on the comments system. My goal is to have 3 comments displayed on each blog post on the main page, and and then a link to go to the main post for the rest and leave your own comment. I’ve figured out how to display all of the comments on the main page, $withcomments=1; but I’m having trouble limiting them to 3 and taking out the comment submission box.

    I’ve looked into making another comment template file, but I have no idea where to start. Could I have some help on this? 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • You will need to work with your theme provider on how to edit the page template in use. Without any specifics, we cannot respond to a generic inquiry.

    Thread Starter Blue_Dragon360

    (@blue_dragon360)

    Alright… I’m not entirely sure what you’re saying. I’m not currently using themes. I can provide code/file structure/screenshots if you need.

    I originally half-built my webpage using HTML and CSS. Deciding that I wanted to integrate wordpress for a convenient CMS and comments system, I researched it, and changed my .html file to .php, and added the necessary code:

    <?php
    		/* Short and sweet */
    		define('WP_USE_THEMES', false);
    
    		?>
    		<?php
    		require('blog/wp-blog-header.php');
    		?>

    I then included the loop where I wanted new posts to display (with some minor edits, of course):

    <?php query_posts('showposts=5'); ?>
    				<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    				<div class="post" id="post-<?php the_ID(); ?>">
    					 <h3 class="storytitle"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    					<div class="meta"> <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?> <?php edit_post_link(__('Edit This')); ?></div>
    
    					<div class="storycontent">
    						<?php the_content(__('(more...)')); ?>
    					</div>
    
    					<div class="feedback">
    				            <?php wp_link_pages(); ?>
    				            <?php comments_popup_link(__('Be the first to comment!'), __('1 Comment'), __('% Comments')); ?>
    					</div>
    
    				</div>
    
    				<?php
    				$withcomments=1;
    				comments_template('/blog/wp-includes/comment-template.php');
    				?>
    
    				<?php endwhile; else: ?>
    				<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    				<?php endif; ?>

    Then I added the $withcomments=1, to show the comments. Unfortunately, this makes it so it shows *all* of the comments, and I can’t find a way to make it just display the most recent 3 comments. This also makes it have a reply box, which I would like to only allow on the post’s page, along with displaying the rest of the comments as usual.

    Is this enough info, or should I post my full code and file structure? I can’t post a link, since I’m running on a locally hosted server.

    to fetch just three comments per post (without comments form), consider to use http://codex.wordpress.org/Function_Reference/get_comments

    Thread Starter Blue_Dragon360

    (@blue_dragon360)

    Ah, that helped! I’ve figured out how to display the last three, but I’ve got another minor problem: Replied comments aren’t in the correct spot. Is there a function or series of functions to detect replies and put them under their thread owners?

    Also, just in case it’s needed, here’s the code I added to my main page in the loop:

    <?php
    				$args = array(
    					'number' => '3',
    					'post_id' => get_the_ID(),
    				);
    				$comments = get_comments($args);
    				foreach($comments as $comment) :
    					echo("<p class='commentAuthor'>".$comment->comment_author . '</p><p class="commentBody">' . $comment->comment_content."</p>");
    				endforeach;
    				?>

    Thread Starter Blue_Dragon360

    (@blue_dragon360)

    Alright, in addition to my problem above, I now have a different problem: The reply link itself isn’t showing up. I’ve tried doing the comment_reply_link();, but I can’t get that to work. Any help? Here’s my updated comments section code (in the loop on my main page):

    <?php
    				$args = array(
    					'number' => '3',
    					'post_id' => get_the_ID(),
    				);
    				$comments = get_comments($args);
    				foreach($comments as $comment) :
    					echo "<div class = 'editLink'>";
    					edit_comment_link('Edit');
    					echo "</div><div class = 'replyLink'>";
    					comment_reply_link();
    					echo "</div>";
    
    					echo("<div class='commentContainer'><div class='avatarContainer'><div class='commentAvatar'>".
    						get_avatar($comment,90).
    						"</div><div class='commentAuthor'>".
    							$comment->comment_author.
    								"</div>");
    					if ( $post = get_post($post_id) ) {
    					            if ( $comment->user_id === $post->post_author ){
    					            	echo ("<div class = 'commentPostAuthor'>Post Author</div>");
    					            }
    
    					        }
    
    									echo ("</div><div class='commentContent'>".
    									$comment->comment_content.
    										"</div></div>" );
    
    				endforeach;
    				?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How can I display 3 comments on the main blog page?’ is closed to new replies.