I thought about 8.2 million hair, but I couldn't help it. the wordpress bulletin board database cache version improves willin's database cache code. When a comment is added, the cache is refreshed. The bulletin board only displays the Administrator's main comments, and the following code is directly placed in functions. in php, and then throw a sentence where the announcement board needs to be displayed? Phpbulletin
I thought about 8.2 million hair, but I couldn't help it. the wordpress bulletin board database cache version improves willin's database cache code. When a comment is added, the cache is refreshed. The bulletin board only displays the Administrator's main comments, and the following code is directly placed in functions. in php, and then throw a sentence where the announcement board needs to be displayed? Php bulletin
I thought about 8.2 million hair, but I couldn't help it.
Wordpress bulletin board database cache
- Improved willin's database cache code and added the cache refresh function when deleting comments.
- Only the Administrator comments are displayed on the bulletin board, but not subcomments.
- The following code is directly placed in functions. php, and then you can throw a sentence where you need to display the announcement board.
.
Before usingpost_id' => 1884
1884 to your bulletin board page id
Function bulletin () {if (! $ Announcement = get_option ('clear _ buffer') {$ args = array ('Post _ id' => 1884, 'number' => 5, 'User _ id' => '1', parent => '0'); $ announcement = ''; $ comments = get_comments ($ args ); foreach ($ comments as $ comment) {$ announcement. ='
'. Human_time_diff (get_comment_date ('U', $ comment-> comment_ID), current_time ('timestamp ')). 'pre '. ''. get_comment_date ('H: I ', $ comment-> comment_ID ). '-comment_post_ID ). "# comment -". $ comment-> comment_ID. '"title = "'. strip_tags ($ comment-> comment_content ). '"> '. convert_smilies ($ comment-> comment_content ).'';} Update_option ('clear _ buffer', $ announcement);} echo $ announcement;} function clear_buffer_function () {update_option ('clear _ buffer ',''); // clear cache} add_action ('comment _ Post', 'clear _ buffer_function '); // release a new comment and clear the cache add_action ('edit _ comment ', 'Clear _ buffer_function '); // The comment is edited. The add_action ('trashed _ comment', 'clear _ buffer_function') is cleared. // The comment is moved to the recycle bin to clear the cache.Principle.
You can use update_option to add tables to the database for caching. This code is universal. If you want to implement caching for other functions, you only need to refer to this example. For example, the original code on the bulletin board is:
Function bulletin () {$ args = array ('Post _ id' => 1884, 'number' => 5, 'user _ id' => '1 ', parent => '0'); $ announcement = ''; $ comments = get_comments ($ args); foreach ($ comments as $ comment) {$ announcement. ='
'. Human_time_diff (get_comment_date ('U', $ comment-> comment_ID), current_time ('timestamp ')). 'pre '. ''. get_comment_date ('H: I ', $ comment-> comment_ID ). '-comment_post_ID ). "# comment -". $ comment-> comment_ID. '"title = "'. strip_tags ($ comment-> comment_content ). '"> '. convert_smilies ($ comment-> comment_content ).'';} Echo $ announcement ;}
You can compare the code at the beginning of the article to find the fault, and find the fault (that is, the place I marked in red) is the general code of the database cache.
Similarly, more functions can be cached. To Cache different functions, you only needif (!$announcement = get_option('clear_buffer'))
Changeif (!$announcement = get_option('clear_buffer2'))
,
Then
function clear_buffer_function() {update_option('clear_buffer', ''); }
Change
Function clear_buffer_function () {update_option ('clear _ buffer', ''); update_option ('clear _ buffer2 ',''); // and so on}
Other places do not move.
Benefits
Today's code is a bit long and complicated. In order to reward all the friends who have carefully read it, send a group of benefits...
Original article address: Cache version of the wordpress bulletin board database. Thank you for sharing it with me.