Improve your WordPress skills with this comprehensive tutorial that includes instructions on creating a post template, adding a comment form, and customizing the comments' CSS, complete with detailed steps and visual aids.
This exercise is excerpted from Noble Desktop’s past WordPress training materials and is compatible with WordPress updates through 2020. To learn current skills in WordPress, check out our WordPress Bootcamp and coding bootcamps in NYC and live online.
Topics covered in this WordPress tutorial:
Making the post template, Adding a comment form, Customizing the comments’ CSS
Exercise Preview
Exercise Overview
Right now, when someone clicks a blog post title, it brings them to a dedicated page for that post, but we can’t see or leave comments. By default, WordPress uses index.php for every page, unless you provide it with other page templates. The reserved template filename for a single post is single.php, which is what we’ll create in this exercise.
If You Did Not Do the Previous Exercise
- In a web browser, go to localhost:8888/mrp/wp-admin (Mac) or localhost/mrp/wp-admin (Windows) and log in if prompted.
- On the left of the Dashboard, mouse over Appearance and click Themes.
- Click the Add New button.
- Click the Upload link, then the Browse (or Choose File) button.
- Navigate to Desktop > Class Files > WordPress.org Class and double–click mrpTheme-ready-for-single-page.zip to open it.
- Click Install Now, then click Activate.
Creating single.php
We want single.php to be very similar to the index.php page, so it will be best to make a duplicate.
In a code editor, open index.php from the mrpTheme folder if it isn’t already open.
- Save the file as single.php into the mrpTheme folder located here:
- Mac: Hard Drive > Applications > MAMP > htdocs > mrp > wp-content > themes
- Windows: C: > xampp > htdocs > mrp > wp-content > themes
- This page doesn’t need everything we created in index.php. Delete the following:
-
<
h1>
title -
<
a>
link around the<
h2>
title (but leave the title) - comments_popup_link
()
-
#
postsNavLink div (and everything inside) - else statement
- no posts found paragraph
-
-
When finished, you should be left with a fairly simple loop, as shown below:
<?php get_header(); ?> <div id="primary"> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_content(); ?> <p class="entry-meta"><?php the_time( 'F j, Y' ); ?><?php the_tags( ' - Tags: ' ); ?></p> <?php endwhile; ?> <?php endif; ?> </div><!-- end primary --> <?php get_sidebar(); ?> <?php get_footer(); ?>
-
The main reason why we wanted a custom post page is for comments. Add the following function and wrapper div to pull in the comment form:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_content(); ?> <p class="entry-meta"><?php the_time( 'F j, Y' ); ?><?php the_tags( ' - Tags: ' ); ?></p> <div id="commentsSection"><?php comments_template(); ?></div> <?php endwhile; ?> <?php endif; ?>
NOTE: This will output the default comment section, with a submission form and the list of comments.
Save the file.
Viewing & Styling the Comments
This is the default comments template, and we don’t have any styles for it. Since we can’t see the code until the page renders, we will use Chrome’s Developer Tools to inspect it to see what our styles should target.
- Open up Chrome and go to:
- Mac: localhost:8888/mrp
- Windows: localhost/mrp
Click the Hello world! post to take a look at the comments template.
-
Ctrl–click (Mac) or Right–click (Windows) to the left of the comment’s bullet, and choose Inspect.
-
In the left-hand panel, the ordered list
<
ol class="
commentlist">
should be highlighted. Hover over it to see the padding and margins highlighted above.OK, we need to get rid of padding and margins on the .commentlist; we’ll remember that. Let’s figure out how to get rid of the bullet.
-
Ctrl–click (Mac) or Right–click (Windows) on the bullet itself and choose Inspect.
In the left-hand panel, a list item
<
li>
should be highlighted. -
In the CSS section on the right, notice that we have a general li style that adds a bullet background image and some padding. That’s what we need to override.
Let’s go make those changes. Leave Chrome open and switch to your code editor.
Open style.css from the mrpTheme folder.
-
At the very bottom, after the .prevPage rule (around line 447), add the following rules. (If you don’t have the .prevPage rule, add the new rules after .imgLeft around line 436):
.commentlist { padding: 0px; margin: 0px; } .commentlist li { background-image: none; padding: 0px; }
Save the file.
Switch back to Chrome. It should still be open to the MRP site. If not, go there now and click the Hello world! title.
Reload the page to see the new styles. Awesome!
Bonus: Finishing the Comment Styling
If you have extra time, you can continue to style the comments.
- If you closed Chrome, open it again and go to:
- Mac: localhost:8888/mrp
- Windows: localhost/mrp
Click the Hello world! post to see the comments.
Leave a new comment so that we have a couple to look at.
Ctrl–click (Mac) or Right–click (Windows) one of the comments’ text and choose Inspect.
-
Note that the entire comment is in a div with the class comment-body.
Above the paragraph, hover over the
<
div class="
comment-meta commentmetadata">
. Above, you can see that .comment-meta styles the date and time of the comment.Below the paragraph, hover over the
<
div class="
reply">
. Above, you can see that .reply styles the reply link below the comment.Let’s make styles for these. Switch back to your code editor.
Open style.css from the mrpTheme folder.
-
We’ll start with the comment-body div. We want some padding, a background color, and some space between comments. After the .commentlist li rule, add the following rule (around line 455):
.comment-body { background-color: #f9f9f9; padding: 10px; margin-bottom: 10px; }
-
Next, we’ll style the comment time and date to be a little smaller. After the .comment-body rule, add the following rule:
.comment-meta { font-size: 13px; font-style: italic; line-height: 15px; margin-bottom: 10px; }
-
We want the reply link in the right corner. After the .comment-meta rule, add the following bold code:
.reply { float: right; }
-
The comment paragraphs need to be a little smaller too. After the .reply rule, add the following rule:
.comment-body p { font-size: 13px; }
Save the file.
Switch back to Chrome and reload the page to get the new styles.
Excellent, with one small problem. The floated Reply link is collapsing the div a bit. Switch back to your code editor.
-
Edit the .comment-body style around line 455 by adding the following bold code:
.comment-body { background-color: #f9f9f9; padding: 10px; margin-bottom: 10px; overflow: hidden; }
Save the file.
Switch back to Chrome and reload the page to get the new styles.
Close the Developer Tools console.
Preview the page in Firefox.
Scroll down to the bottom and notice that the large comment text field extends beyond its containing div into the sidebar. Let’s fix this.
Ctrl–click (Mac) or Right–click (Windows) the large text field, and choose Inspect.
Notice that the text field has an ID of comment and is inside a form that has an ID of commentform. This is how we will target our style.
Switch back to your code editor.
-
After the comment-body p rule, add the following rule (around line 473):
#commentform #comment { width: 100%; }
Save the file.
Switch back to Firefox and reload the page to see that the text field no longer extends beyond its containing div. Sweet!