Retrieve the floor or serial number from the WordPress comment list

Source: Internet
Author: User
Tags comments php file

In our comment list, we occasionally want to get the serial number of this comment. If we can get the serial number according to certain rules, we can add some new information for a specific comment, for example, add a floor for a comment or add an advertisement after 5th comments. However, wordpress does not provide this function. We cannot have a global parameter in mytheme_comment to obtain the comment sequence number of each article. The article about how to build your own comment list and how to design your wordpress comment list and comment box is very clean. If you are already very clear about the building problem, you will find the problems mentioned in this article.

Let's achieve the specific objectives of this article.

Serial number mechanism & uarr;
Wordpress does not provide an independent sequence number for each article, but we cannot simply use comment_ID to replace this sequence number. This does not reflect the position of the comment in this article. Therefore, we can only use php to implement the GLOBAL parameter setting method of $ GLOBAL.

The code is as follows: Copy code
If (! $ GLOBALS ['current _ comment_order ']) {
$ GLOBALS ['current _ comment_order '] = 1;
} Else {
$ GLOBALS ['current _ comment_order '] ++;
If ($ GLOBALS ['current _ comment_order ']> $ args ['per _ page']) $ GLOBALS ['current _ comment_order'] = 0;
}

The code above sets $ GLOBALS ['current _ comment_order '] to 1 if it does not exist. If it already exists, add 1 to the original one. If it reaches the maximum number, the command is 0. As to why $ GLOBAL is used, there is no doubt that wordpress does not have a GLOBAL parameter to obtain the serial number.

How to Use & uarr;
Like the article mentioned above, you need to design your own comment list. One is to process the comments. php file, and add a function in functions. php to present the list. This function will be called directly by wp_list_comments without adding a hook. We call this function mytheme_comment ($ comment, $ arg, $ depth). It not only does not need to add hooks, but also does not need to close list tags, for example, if you want to use <li> to present the list, you should not write </li>, but make <li> open, because you need to know that you may display your comments in nesting mode, wordpress will automatically close it for you.

The code is as follows: Copy code
Function mytheme_comment ($ comment, $ args, $ depth ){
$ Comment_id = $ comment-> comment_ID;
$ Comment_author = $ comment-> comment_author;
$ Comment_parent = $ comment-> comment_parent;
$ Comment_post = $ comment-> comment_post_ID;
?>
<Li <? Php comment_class ($ replytocom. $ current);? >>
<Div id = "comment-<? Php comment_ID ()?> ">
<Div> <? Php echo get_avatar ($ comment, $ size = '40');?> </Div>
<Div>
<Span> <? Php echo get_comment_author_link () ;?> </Span>
<Span >#<? Php comment_ID ();?> Building </span>
<? Php if ($ comment_parent) echo '<span> returns to <a href = "# comment -'. $ comment_parent. '"rel =" nofollow "> @'. $ comment_parent. 'Floor </a> </span>';?>
<Span> <? Php echo get_comment_date ('Y/m/D');?> </Span>
<Span> <? Php echo get_comment_time ('H: I: s');?> </Span>
<Span> <a href = "<? Php echo get_permalink ($ comment_post);?> & Replytocom = <? Php comment_ID ();?> # Respond "rel =" nofollow "data-comment-id =" <? Php comment_ID ();?> "Data-comment-author =" <? Php echo $ comment_author;?> "> Reply </a> </span>
<Span> <? Php edit_comment_link ('edit', '','')?> </Span>
</Div>
<Div>
<? Php comment_text ();?>
<? Php if ($ comment-> comment_approved = '0') printf ('<div> % s </div>', 'your opinion is under review, it will soon appear in the comment list ~~ ');?>
<Div> </div>
</Div>
<? Php
}

For the three parameters, you only need to know the wp_list_comments function. In short, they are useful.

Combine depth with floor & uarr;
With this idea, you only need to take the first layer in the nested comment list as the computing object, and each nested layer is counted as the first floor. In this way, you can add a short advertisement code behind the first floor. The following code is used:

The code is as follows: Copy code
Function mytheme_comment ($ comment, $ args, $ depth ){
If ($ depth = 1): // if you need to calculate all layers 1 and 2, you can try ($ depth = 1 | $ depth = 2)
If (! $ GLOBALS ['current _ comment_order ']) {
$ GLOBALS ['current _ comment_order '] = 1;
} Else {
$ GLOBALS ['current _ comment_order '] ++;
If ($ GLOBALS ['current _ comment_order ']> $ args ['per _ page']) $ GLOBALS ['current _ comment_order'] = 0;
        }
If ($ GLOBALS ['current _ comment_order '] = 1): // This indicates the first floor. if you want to represent the second floor, change the value after the full equal sign to 2.
?> Your advertisement code <? Php
Endif;
Endif;
}

What should I do when turning pages? & uarr;
When there are too many comments, page turning is inevitable. Fortunately, wordpress already provides some materials required for page turning, the current page number, and the total page number, you can obtain the number of entries on each page in one way:

The code is as follows: Copy code
Function mytheme_comment ($ comment, $ args, $ depth ){
$ Current_page = $ args ['Page'];
$ Totle_page = get_comment_pages_count ();
$ Per_page_num = $ args ['per _ page'];
$ Totle_num = get_comments_number ();
}

When turning pages, the first thing you should consider is that you plan to display only a code on the first page or on the page, and use the following judgment

If ($ current_page = 2): // if the current comment is the second page, it is usually reflected in the URL as comment-page-2, and if you want to calculate a general position, for example, you want to display a code segment in the first 40th lines, you can combine the number of entries on each page with the current page number to determine, for example

The code is as follows: Copy code
If ($ per_page_num * ($ current_page-1) + $ GLOBAL ['current _ comment_order '] = 40 ):

Of course, what you may need is that when $ GLOBAL ['current _ comment_order '] is 40, that is

The code is as follows: Copy code
If ($ GLOBAL ['current _ comment_order '] = 40 ):

However, this is really not feasible, because $ GLOBAL ['current _ comment_order '] itself records the number of comments on the current page, so we can honestly make a decision.

In short, we use $ GLOBAL ['current _ comment_order '] to add a floor. The core code is the first code, then you can combine the article introduced at the beginning of the article to make more powerful calling functions.

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.