The whole process of WordPress theme creation (10): making comments.php
Feb 21, 2023 am 10:12 AMI introduced you to "The whole process of WordPress theme production (9): Making single.php". This article continues to introduce to you how to make comments.php. Let's take a look at it together~
Today we will make the comment module of the comment theme. Create a new comments.php under the theme directory Aurelius, cut the following code in single.php, and paste it into comments.php:
<!– Comment’s List –> <h3>Comments</h3> <div class="hr dotted clearfix">?</div> <ol class="commentlist"> <li class="comment"> <div class="gravatar">?<img alt="" src=’images/gravatar.png’ height=’48′ width=’48′ />?<a class="comment-reply-link" href=">Reply</a>?</div> <div class="comment_content"> <div class="clearfix">?<cite class="author_name"><a href="">Joe?Bloggs</a></cite> <div class="comment-meta commentmetadata">January?6,?2010?at?6:26?am</div> </div> <div class="comment_text"> <p>Donec?leo.?Aliquam?risus?elit,?luctus?vel,?interdum?vitae,?malesuada?eget,?elit.?Nulla?vitae?ipsum.?Donec?ligula?ante,?bibendum?sit?amet,?elementum?quis,?viverra?eu,?ante.?Fusce?tincidunt.?Mauris?pellentesque,?arcu?eget?feugiat?accumsan,?ipsum?mi?molestie?orci,?ut?pulvinar?sapien?lorem?nec?dui.</p> </div> </div> </li> </ol> <div class="hr clearfix">?</div> <!– Comment Form –> <form id="comment_form" action="" method="post"> <h3>Add?a?comment</h3> <div class="hr dotted clearfix">?</div> <ul> <li class="clearfix"> <label for="name">Your?Name</label> <input id="name" name="name" type="text" /> </li> <li class="clearfix"> <label for="email">Your?Email</label> <input id="email" name="email" type="text" /> </li> <li class="clearfix"> <label for="email">Your?Website</label> <input id="website" name="website" type="text" /> </li> <li class="clearfix"> <label for="message">Comment</label> <textarea id="message" name="message" rows="3" cols="40"></textarea> </li> <li class="clearfix"> <!– Add Comment Button –> <a type="submit" class="button medium black right">Add?comment</a>?</li> </ul> </form>
Add the code in the original location of single.php:
<?php comments_template(); ?>
The above statement The function is to import all the contents in comments.php into single.php, which has the same effect as writing the code in comments.php directly in single.php.
For the sake of security, to prevent malicious users from opening the comment file directly, please add the following code in the comments.php header:
<?php if (isset($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME'])) die ('Please do not load this page directly. Thanks!'); ?>
Because the comment code output by WordPress's output comment function wp_list_comments() Different from the comment code of our theme, we have to customize our comment list and delete the following code in comments.php (the following code is used to list all comments on the article):
<li class="comment"> <div class="gravatar">?<img alt="" src=’images/gravatar.png’ height=’48′ width=’48′ />?<a class="comment-reply-link" href=">Reply</a>?</div> <div class="comment_content"> <div class="clearfix">?<cite class="author_name"><a href="">Joe?Bloggs</a></cite> <div class="comment-meta commentmetadata">January?6,?2010?at?6:26?am</div> </div> <div class="comment_text"> <p>Donec?leo.?Aliquam?risus?elit,?luctus?vel,?interdum?vitae,?malesuada?eget,?elit.?Nulla?vitae?ipsum.?Donec?ligula?ante,?bibendum?sit?amet,?elementum?quis,?viverra?eu,?ante.?Fusce?tincidunt.?Mauris?pellentesque,?arcu?eget?feugiat?accumsan,?ipsum?mi?molestie?orci,?ut?pulvinar?sapien?lorem?nec?dui.</p> </div> </div> </li>
Change to :
<?php if (!empty($post->post_password)?&&?$_COOKIE['wp-postpass_'?.?COOKIEHASH]?!=?$post->post_password)?{? ????????//?if?there's?a?password ????????//?and?it?doesn't?match?the?cookie ?????> ????<li class="decmt-box"> ????????<p><a href="#addcomment">請輸入密碼再查看評論內(nèi)容.</a></p> ????</li> ????<?php } else if ( !comments_open() ) { ?> ????<li class="decmt-box"> ????????<p><a href="#addcomment">評論功能已經(jīng)關(guān)閉!</a></p> ????</li> ????<?php } else if ( !have_comments() ) { ?> ????<li class="decmt-box"> ????????<p><a href="#addcomment">還沒有任何評論,你來說兩句吧</a></p> ????</li> ????<?php } else { wp_list_comments('type=comment&callback=aurelius_comment'); } ?>
You can roughly see the meaning of the above code, which is a lot of if... then..., if the above conditions are not met, all comments will be listed. Now change the ?> in functions.php in the theme folder Aurelius to the following code. If the functions.php you downloaded from this blog before already has the following code, there is no need to add it:
function?aurelius_comment($comment,?$args,?$depth)? { ???$GLOBALS['comment']?=?$comment;??> ???<li class="comment" id="li-comment-<?php comment_ID(); ?>"> <div class="gravatar">?<?php if (function_exists('get_avatar') && get_option('show_avatars')) { echo get_avatar($comment, 48); } ?> ?<?php comment_reply_link(array_merge( $args, array('reply_text' =>?'回復(fù)','depth'?=>?$depth,?'max_depth'?=>?$args['max_depth'])))??>?</div> <div class="comment_content" id="comment-<?php comment_ID(); ?>"> <div class="clearfix"> <?php?printf(__('<cite class="author_name">%s</cite>'),?get_comment_author_link());??> <div class="comment-meta commentmetadata">發(fā)表于:<?php echo get_comment_time('Y-m-d H:i'); ?></div> ???<?php edit_comment_link('修改'); ?> </div> <div class="comment_text"> <?php if ($comment->comment_approved?==?'0')?:??> <em>你的評論正在審核,稍后會顯示出來!</em><br /> ??????<?php endif; ?> ??????<?php comment_text(); ?> </div> </div> <?php } ?>
WordPress functions used in the above code and corresponding descriptions:
Function name | Function function |
get_avatar($comment, 48) | Get the commenter’s gravatar avatar, the size is 48 * 48 |
##comment_reply_link() | Link to reply to the message|
Used to get the commenter’s blog address | |
Get the comment publishing time | |
The link for the administrator to modify the comment | |
Output comment content |
<!– Comment Form –> <form id="comment_form" action="" method="post"> <h3>Add?a?comment</h3> <div class="hr dotted clearfix">?</div> <ul> <li class="clearfix"> <label for="name">Your?Name</label> <input id="name" name="name" type="text" /> </li> <li class="clearfix"> <label for="email">Your?Email</label> <input id="email" name="email" type="text" /> </li> <li class="clearfix"> <label for="email">Your?Website</label> <input id="website" name="website" type="text" /> </li> <li class="clearfix"> <label for="message">Comment</label> <textarea id="message" name="message" rows="3" cols="40"></textarea> </li> <li class="clearfix"> <!– Add Comment Button –> <a type="submit" class="button medium black right">Add?comment</a>?</li> </ul> </form>
and change it to:
<?php if ( !comments_open() ) : // If registration required and not logged in. elseif ( get_option('comment_registration') && !is_user_logged_in() ) : ?> <p>你必須?<a href="<?php echo wp_login_url( get_permalink() ); ?>">登錄</a>?才能發(fā)表評論.</p> <?php else : ?> <!-- Comment Form --> <form id="commentform" name="commentform" action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php"?method="post"> ????<h3>發(fā)表評論</h3> ????<div class="hr dotted clearfix">?</div> ????<ul> ????????<?php if ( !is_user_logged_in() ) : ?> ????????<li class="clearfix"> ????????????<label for="name">昵稱</label> ????????????<input type="text" name="author" id="author" value="<?php echo $comment_author; ?>"?size="23"?tabindex="1"?/> ????????</li> ????????<li class="clearfix"> ????????????<label for="email">電子郵件</label> ????????????<input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>"?size="23"?tabindex="2"?/> ????????</li> ????????<li class="clearfix"> ????????????<label for="email">網(wǎng)址(選填)</label> ????????????<input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>"?size="23"?tabindex="3"?/> ????????</li> ????????<?php else : ?> ????????<li class="clearfix">您已登錄:<a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>.?<a href="<?php echo wp_logout_url(get_permalink()); ?>"?title="退出登錄">退出??</a></li> ????????<?php endif; ?> ????????<li class="clearfix"> ????????????<label for="message">評論內(nèi)容</label> ????????????<textarea id="message comment" name="comment" tabindex="4" rows="3" cols="40"></textarea> ????????</li> ????????<li class="clearfix"> ????????????<!-- Add Comment Button --> ????????????<a href="javascript:void(0);" onClick="Javascript:document.forms['commentform'].submit()" class="button medium black right">發(fā)表評論</a>?</li> ????</ul> ????<?php comment_id_fields(); ?> ????<?php do_action('comment_form', $post->ID);??> </form> <?php endif; ?>
Function | |
Determine whether the user is logged in | |
Blog login address | ##get_comment_author_link |
Used to get the commenter’s blog address | $comment_author |
##$ comment_author_email | Read cookie, if the user has posted a comment before, it will automatically help the user fill in Email |
$comment_author_url | Read cookie, if If the user has made a comment before, it will automatically help the user fill in the blog address |
do_action('comment_form', $post->ID); | This function is a Some plug-ins reserve |
wp_logout_url | Logout link |
Recommended learning: " | WordPress Tutorial
The above is the detailed content of The whole process of WordPress theme creation (10): making comments.php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

Web development design is a promising career field. However, this industry also faces many challenges. As more businesses and brands turn to the online marketplace, web developers have the opportunity to demonstrate their skills and succeed in their careers. However, as demand for web development continues to grow, the number of developers is also increasing, resulting in increasingly fierce competition. But it’s exciting that if you have the talent and will, you can always find new ways to create unique designs and ideas. As a web developer, you may need to keep looking for new tools and resources. These new tools and resources not only make your job more convenient, but also improve the quality of your work, thus helping you win more business and customers. The trends of web development are constantly changing.

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

Do you want to connect your website to Yandex Webmaster Tools? Webmaster tools such as Google Search Console, Bing and Yandex can help you optimize your website, monitor traffic, manage robots.txt, check for website errors, and more. In this article, we will share how to add your WordPress website to the Yandex Webmaster Tool to monitor your search engine traffic. What is Yandex? Yandex is a popular search engine based in Russia, similar to Google and Bing. You can excel in Yandex

Do you want to know how to use cookies on your WordPress website? Cookies are useful tools for storing temporary information in users’ browsers. You can use this information to enhance the user experience through personalization and behavioral targeting. In this ultimate guide, we will show you how to set, get, and delete WordPresscookies like a professional. Note: This is an advanced tutorial. It requires you to be proficient in HTML, CSS, WordPress websites and PHP. What are cookies? Cookies are created and stored when users visit websites.

Do you need to fix HTTP image upload errors in WordPress? This error can be particularly frustrating when you create content in WordPress. This usually happens when you upload images or other files to your CMS using the built-in WordPress media library. In this article, we will show you how to easily fix HTTP image upload errors in WordPress. What is the reason for HTTP errors during WordPress media uploading? When you try to upload files to Wo using WordPress media uploader

To create an account on WordPress, simply visit its website, select the registration option, fill in the registration form, and verify your email address. Other ways to register include using a Google account or Apple ID. The benefits of signing up include creating a website, gaining features, joining the community, and gaining support.
