Wordpress topic creation tutorial (11): comment Template comments. php

Source: Internet
Author: User
We have created a single-page template in the previous article. we can find the code and index of the single-page template. php is similar, but today we make them a little different. we add a comment form to the article template so that visitors can post comments.

We have created a single-page template in the previous article. we can find the code and index of the single-page template. php is similar, but today we make them a little different. we add a comment form to the article template so that visitors can post comments.

First, create a file comments. php under the topic folder, open the single. php file, cut out the comment code in it, and paste it into the comments. php file. the code to be cut is as follows:

      Comments           
 
 
  1. Reply Joe Bloggs January 6, 2010 at 6:26 am

    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.

 

Then add the code in the original location of the single. php file:

The default comments_template () function is to load comments under the topic folder. php file. This function can also contain parameters so that you can load other files. for example, if you need to load a different comment form on some pages, you need to use comments_template () with parameters, I will not elaborate here.
To prevent some malicious users from directly opening the comment file, we add code in the comments. php header:

 

Modify comment list

Wordpress has the function wp_list_comments () that automatically outputs the comment list. Therefore, we delete the original comment list code and change it to this function. However, we need to add some judgment functions, for example, when a comment requires a password to be viewed, the comment has been closed, and the comment has not been commented, different outputs are required. Therefore, the original comment code should be:

 

  • Reply Joe Bloggs January 6, 2010 at 6:26 am

    Donec leo. Aliquam risus elit, luctus vel, interdum vitae, malesuada eget, elit. Nulla vitae ipsum. Donec ligul a 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.

  •  

    Replace:

     Post_password) & $ _ COOKIE ['WP-postpass _ '. COOKIEHASH]! = $ Post-> post_password) {// if there's a password // and it doesn't match the cookie?>
  • Enter the password to view the comment.

  • The comment function is disabled!

  • There are no comments yet. just give me a few comments.

  • In the above wp_list_comments function, we have two parameters in our house. type = comment only means to output comments. in addition to comments, there are also pings \ trackback \ pingback and so on, callback = aurelius_comment indicates that a custom function aurelius_comment is called to display comments.
    Custom functions must be added to the functions. php file of the topic. therefore, see "?>" in functions. php" Add the following code. if the following code already exists in your functions. php file, do not add it again:

    Function aurelius_comment ($ comment, $ args, $ depth) {$ GLOBALS ['comment'] = $ comment;?>
  • 'Reply ', 'dest' => $ depth, 'max _ depth' => $ args ['max _ depth '])?> "> % S'), Get_comment_author_link ();?> Posted on: Comment_approved = '0'):?>Your comment is under review and will be displayed later!
  •  

     

    The following describes the functions used in the preceding user-defined functions:

     

     

     
     
     

    The code comment added above can be correctly displayed. Next, add the form for submitting the comment.
    Comment Form
    Delete the comment form code in comments. php:

     

       

     

     

    In fact, you do not need to manually enter each form item. The new wordprss provides a very convenient function: comment_form (). add the following code:

     

               
       

    '', 'Label _ submit '=> _ ('submit comments'), 'comment _ notes_after '=>''); comment_form ($ defaults); endif; else:?>

     

    You can see that the above code also adds a judgment to check whether comments are allowed and whether you need to log on to make comments.
    You can use the parameters of the comment_form () function together with css to output a personalized form.

     

     

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.