PHP code instance for adding floor numbers to messages in WordPress

Source: Internet
Author: User
This article mainly introduces the PHP code example for adding a floor number to a message in WordPress. here, it is only for the main comment, not for the comment in the stacked middle floor, if you need a friend, you can refer to the blog comment page that was suddenly found to have a problem recently. The previous setting was "display new comments on the top of each page", which is called "Display Comments in reverse order, however, the topic only supports regular comment floors, so the floor and floor number cannot be matched. I searched in zww. me found that there is implemented code, but it cannot work normally after it is added to the blog. for example, when the limit is displayed on 25 pages, the article only shows the 25 th floor when there is a comment. I made a record for your reference.

Find $ GLOBALS ['comment'] = $ comment in the topic file functions. php. add the following code to the end:

/* Main comment counter */global $ commentcount, $ wpdb, $ post; if (! $ Commentcount) {// initialize the floor counter if (get_option ('comment _ order') = 'desc ') {// Reverse order $ comments = $ wpdb-> get_results ("SELECT * FROM $ wpdb-> comments WHERE comment_post_ID = $ post-> id and comment_type ='' AND comment_approved = '1 'AND! Comment_parent "); $ cnt = count ($ comments); // obtain the total number of primary comments $ page = get_query_var ('cpage '); // Obtain the current comment list page $ cpp = get_option ('comments _ per_page '); // Obtain the number of comments displayed per page if (ceil ($ cnt/$ cpp) = 1 | ($ page> 1 & $ page = ceil ($ cnt/$ cpp) {$ commentcount = $ cnt + 1; // if a comment has only one or the last page, the initial value is the total number of comments.} else {$ commentcount = $ cpp * $ page + 1 ;}} else {// order $ page = get_query_var ('cpage')-1; $ cpp = get_option ('comments _ per_page' ); // Get comments per page $ commentcount = $ cpp * $ page ;}}/* primary comment counter end */if (! $ Parent_id = $ comment-> comment_parent) {$ commentcountText ='

'; If (get_option ('comment _ order') = 'desc') {// Reverse order $ commentcountText. = -- $ commentcount. 'loudd';} else {switch ($ commentcount) {case 0: $ commentcountText. = 'sofa! '; ++ $ Commentcount; break; case 1: $ commentcountText. =' bench! '; ++ $ Commentcount; break; case 2: $ commentcountText. =' floor! '; ++ $ Commentcount; break; default: $ commentcountText. =++ $ commentcount. 'loud'; break ;}}$ commentcountText. ='';}}

Add the following code to the proper position to output the floor number.

<? Php echo $ commentcountText; // main comment floor No.-by zwwooooo?>

The modified code should be like this (take the latest official wp_list_comments () callback function code as an example ):

<? Phpfunction mytheme_comment ($ comment, $ args, $ depth) {$ GLOBALS ['comment'] = $ comment;/* primary comment counter by zwwooooo Modified Gimhoy ( http://blog.gimhoy.com ) */Global $ commentcount, $ wpdb, $ post; if (! $ Commentcount) {// initialize the floor counter if (get_option ('comment _ order') = 'desc ') {// Reverse order $ comments = $ wpdb-> get_results ("SELECT * FROM $ wpdb-> comments WHERE comment_post_ID = $ post-> id and comment_type ='' AND comment_approved = '1 'AND! Comment_parent "); $ cnt = count ($ comments); // obtain the total number of primary comments $ page = get_query_var ('cpage '); // Obtain the current comment list page $ cpp = get_option ('comments _ per_page '); // Obtain the number of comments displayed per page if (ceil ($ cnt/$ cpp) = 1 | ($ page> 1 & $ page = ceil ($ cnt/$ cpp) {$ commentcount = $ cnt + 1; // if a comment has only one or the last page, the initial value is the total number of comments.} else {$ commentcount = $ cpp * $ page + 1 ;}} else {// order $ page = get_query_var ('cpage')-1; $ cpp = get_option ('comments _ per_page' ); // Get comments per page $ commentcount = $ cpp * $ page ;}}/* primary comment counter end */if (! $ Parent_id = $ comment-> comment_parent) {$ commentcountText ='

'; If (get_option ('comment _ order') = 'desc') {// Reverse order $ commentcountText. = -- $ commentcount. 'loudd';} else {switch ($ commentcount) {case 0: $ commentcountText. = 'sofa! '; ++ $ Commentcount; break; case 1: $ commentcountText. =' bench! '; ++ $ Commentcount; break; case 2: $ commentcountText. =' floor! '; ++ $ Commentcount; break; default: $ commentcountText. =++ $ commentcount. 'loud'; break ;}}$ commentcountText. ='';}} Extract ($ args, EXTR_SKIP); if ('P' = $ args ['style']) {$ tag = 'p '; $ add_below = 'comment';} else {$ tag = 'Lil'; $ add_below = 'P-comment';}?> <? Php echo $ tag?> <? Php comment_class (empty ($ args ['has _ children '])? '': 'Parent')?> Id = "comment-<? Php comment_ID ()?> "> <? Php if ('P '! = $ Args ['style']):?>

<? Php endif;?>

<? Php if ($ args ['Avatar _ size']! = 0) echo get_avatar ($ comment, $ args ['Avatar _ size']);?> <? Php printf (__('% SSays: '), get_comment_author_link ()?>

<? Php if ($ comment-> comment_approved = '0'):?><? Php _ e ('Your comment is awaiting moderation. ')?>
<? Php endif;?>

Comment_ID)?> "> <? Php/* translators: 1: date, 2: time */printf (_ ('% 1 $ s at % 2 $ s'), get_comment_date (), get_comment_time ()?> <? Php edit_comment_link (_ ('(Edit)'), '','');?>

<? Php comment_text ()?>

<? Php comment_reply_link (array_merge ($ args, array ('add _ below '=> $ add_below, 'dest' => $ depth, 'max _ depth '=> $ args ['max _ depth'])?>

<? Php echo $ commentcountText; // main comment floor No.-by zwwooooo?> <? Php if ('P '! = $ Args ['style']):?>

<? Php endif;?> <? Php}

Add the style by yourself ~~

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.