Today in class we worked on adding comments to the single view for a post. On the homepage (which for now is index.php) you see a list of posts so there is no room to show the actual comments, but it is good to see the number of comments. When you click a post to view it by itself, then you want to see the comments for that post.
Review of how WordPress puts together a page
Stepping back for a minute there are three basic kinds of template files. These are my classifications and not official WordPress ones. In WP they are all called Template Files.
- Main Template Pages: These are what WP calls to render a given page. The default is always index.php. Other examples include:
- index.php: defaults to this if no other Main Template Page is found
- single.php: Shows a single Post
- page.php: Shows a static Page
- home.php: Shows the home page
- search.php: shows the search results
- Official Template Blocks:these are parts of a page that are part of the official WP structure. They have template tags that are used to include them:
- header.php: includes header and beginning of body. Called by get_header();
- footer.php: includes the footer with closing body. called by get_footer();
- sidebar.php: includes sidebars (they must be registered in functions.php and content added in dashboard). called by get_sidebar();
- comments.php: shows the comments. called by comments_template()
- Custom Template Parts:these are parts of a page you can create yourself. The name of the file is up to you. We have used it to store code to show content inside of the Loop. Remember the names of these files is up to you, but should be based on what they do.
- content.php: we made this to show the content of the basic loop in index.php. called by get_template_part(‘content’);
- content-single.php: we made this to show the content of a single post. called by get_template_part(‘content’, ‘single’);
Here are the files we worked with relating to comments today. THe => represents when the Main Template Page requires a Custom Template Part.
Showing number of comments by a post on the index page
index.php => content.php
In index.php you have the loop and then in content.php we show the post and today added some code to show the number of comments. We added code to content.php to show the number of comments for the post. The main template tag we used was comments_popup_link() which has three arguments to show different text if there are 1) 0 comments, 2) 1 comment 3) more than 1 comment.
<?php comments_popup_link( 'No Comments','1 comment', '% comments'); ?>
We wrapped this in an if statement that checks to see if comments are open for the post:
<?php if( comments_open( )): ?> <?php comments_popup_link( 'No Comments','1 comment', '% comments'); ?> <?php else: ?> comments closed <?php endif; ?>
Moving from the index to the single post view
To show a post by itself we need two new files:
single.php => content-single.php
We created these by saving index.php as single.php and content.php as content-single.php
In single.php the only change you have to make is to call content-single instead of content:
get_template_part( 'content' );
get_template_part( 'content','single' );
content-single.php also has one big change which is to add code to show the comments:
><?php comments_template( '',true) ?>
That code calls comments.php into the page. The rest of the time we worked on comments.php
In comments.php, the main workhorse is wp_list_comments() Template Tag, that pulls in the actual comments. We add around that some HTML and some php to check if there are comments and show the number of comments. Here is our basic comments.php page:
<div id="comments"> <!-- check if there are comments --> <?php if( have_comments() ): ?> <h2 id="comments-title"> There <?php comments_number( 'are no comments','is one comment', 'are % comments' ) ?> </h2> <ol class="comment-list"> <?php wp_list_comments(); ?> </ol> <?php endif; ?> </div>