Home CMS Tutorial WordPress The whole process of WordPress theme creation (10): making comments.php

The whole process of WordPress theme creation (10): making comments.php

Feb 21, 2023 am 10:12 AM
wordpress

I 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~

The whole process of WordPress theme creation (10): making comments.php

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>
Copy after login

Add the code in the original location of single.php:

<?php comments_template(); ?>
Copy after login

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[&#39;SCRIPT_FILENAME&#39;]) && &#39;comments.php&#39; == basename($_SERVER[&#39;SCRIPT_FILENAME&#39;]))
die (&#39;Please do not load this page directly. Thanks!&#39;);
?>
Copy after login

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>
Copy after login

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">请输入密码再查看评论内容.</a></p>
    </li>
    <?php 
        } else if ( !comments_open() ) {
    ?>
    <li class="decmt-box">
        <p><a href="#addcomment">评论功能已经关闭!</a></p>
    </li>
    <?php 
        } else if ( !have_comments() ) { 
    ?>
    <li class="decmt-box">
        <p><a href="#addcomment">还没有任何评论,你来说两句吧</a></p>
    </li>
    <?php 
        } else {
            wp_list_comments(&#39;type=comment&callback=aurelius_comment&#39;);
        }
    ?>
Copy after login

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(&#39;get_avatar&#39;) && get_option(&#39;show_avatars&#39;)) { echo get_avatar($comment, 48); } ?>
 <?php comment_reply_link(array_merge( $args, array(&#39;reply_text&#39; => '回复','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">发表于:<?php echo get_comment_time(&#39;Y-m-d H:i&#39;); ?></div>
   <?php edit_comment_link(&#39;修改&#39;); ?>
</div>
<div class="comment_text">
<?php if ($comment->comment_approved == '0') : ?>
<em>你的评论正在审核,稍后会显示出来!</em><br />
      <?php endif; ?>
      <?php comment_text(); ?>
</div>
</div>
<?php } ?>
Copy after login

WordPress functions used in the above code and corresponding descriptions:

Link to reply to the messageget_comment_author_linkUsed to get the commenter’s blog addressget_comment_time Get the comment publishing time##edit_comment_linkcomment_text()Okay, now it can be displayed normally at the bottom of your article page Commented! Now we continue to create a form for submitting comments. Delete the following code (that is, the code of the comment form):
Function name Function function
get_avatar($comment, 48) Get the commenter’s gravatar avatar, the size is 48 * 48
##comment_reply_link()
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>
Copy after login

and change it to:

<?php 
if ( !comments_open() ) :
// If registration required and not logged in.
elseif ( get_option(&#39;comment_registration&#39;) && !is_user_logged_in() ) : 
?>
<p>你必须 <a href="<?php echo wp_login_url( get_permalink() ); ?>">登录</a> 才能发表评论.</p>
<?php else  : ?>
<!-- Comment Form -->
<form id="commentform" name="commentform" action="<?php echo get_option(&#39;siteurl&#39;); ?>/wp-comments-post.php" method="post">
    <h3>发表评论</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">网址(选填)</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(&#39;siteurl&#39;); ?>/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">评论内容</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[&#39;commentform&#39;].submit()" class="button medium black right">发表评论</a> </li>
    </ul>
    <?php comment_id_fields(); ?>
    <?php do_action(&#39;comment_form&#39;, $post->ID); ?>
</form>
<?php endif; ?>
Copy after login

function Name##is_user_logged_inwp_login_url##get_comment_author_linkRead the cookie and automatically help the user fill in the user name if the user has posted a comment before##$ comment_author_emailRead cookie, if the user has posted a comment before, it will automatically help the user fill in Email$comment_author_urlRead 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 reservewp_logout_urlLogout linkWordPress Tutorial》
Function
Determine whether the user is logged in
Blog login address
Used to get the commenter’s blog address $comment_author
Recommended learning: "

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP vs. Flutter: The best choice for mobile development PHP vs. Flutter: The best choice for mobile development May 06, 2024 pm 10:45 PM

PHP and Flutter are popular technologies for mobile development. Flutter excels in cross-platform capabilities, performance and user interface, and is suitable for applications that require high performance, cross-platform and customized UI. PHP is suitable for server-side applications with lower performance and not cross-platform.

How to change page width in wordpress How to change page width in wordpress Apr 16, 2024 am 01:03 AM

You can easily modify your WordPress page width by editing your style.css file: Edit your style.css file and add .site-content { max-width: [your preferred width]; }. Edit [your preferred width] to set the page width. Save changes and clear cache (optional).

How to create a product page in wordpress How to create a product page in wordpress Apr 16, 2024 am 12:39 AM

Create a product page in WordPress: 1. Create the product (name, description, pictures); 2. Customize the page template (add title, description, pictures, buttons); 3. Enter product information (stock, size, weight); 4 . Create variations (different colors, sizes); 5. Set visibility (public or hidden); 6. Enable/disable comments; 7. Preview and publish the page.

In which folder are wordpress articles located? In which folder are wordpress articles located? Apr 16, 2024 am 10:29 AM

WordPress posts are stored in the /wp-content/uploads folder. This folder uses subfolders to categorize different types of uploads, including articles organized by year, month, and article ID. Article files are stored in plain text format (.txt), and the filename usually includes its ID and title.

Where is the wordpress template file? Where is the wordpress template file? Apr 16, 2024 am 11:00 AM

WordPress template files are located in the /wp-content/themes/[theme name]/ directory. They are used to determine the appearance and functionality of the website, including header (header.php), footer (footer.php), main template (index.php), single article (single.php), page (page.php), Archive (archive.php), category (category.php), tag (tag.php), search (search.php) and 404 error page (404.php). By editing and modifying these files, you can customize the appearance of your WordPress website

Which version of wordpress is stable? Which version of wordpress is stable? Apr 16, 2024 am 10:54 AM

The most stable WordPress version is the latest version because it contains the latest security patches, performance enhancements, and introduces new features and improvements. In order to update to the latest version, log into your WordPress dashboard, go to the Updates page and click Update Now.

How to search for authors in WordPress How to search for authors in WordPress Apr 16, 2024 am 01:18 AM

Search for authors in WordPress: 1. Once logged in to your admin panel, navigate to Posts or Pages, enter the author name using the search bar, and select Author in Filters. 2. Other tips: Use wildcards to broaden your search, use operators to combine criteria, or enter author IDs to search for articles.

What language is used to develop WordPress? What language is used to develop WordPress? Apr 16, 2024 am 12:03 AM

WordPress is developed using PHP language as its core programming language for handling database interactions, form processing, dynamic content generation, and user requests. PHP was chosen for reasons including cross-platform compatibility, ease of learning, active community, and rich library and frameworks. Apart from PHP, WordPress also uses languages ​​like HTML, CSS, JavaScript, SQL, etc. to enhance its functionality.

See all articles