The top data of wordpress is stored in the option_name of the options table, which is equal to the sticky_posts and option_value fields. The data is stored after being serialized, that is, the php function serialize is used.
Retrieve methods in wordpress
$ Sticky = get_option ('sticky _ posts ');
Code of the current homepage pin
Currently, the code for top-up on my homepage is as follows. I feel that many templates will have this problem.
The code is as follows: |
Copy code |
$ Sticky = get_option ('sticky _ posts'); rsort ($ sticky ); Query_posts (array ('post _ in' => $ sticky, 'Caller _ get_posts' => 1, 'showposts' => 4 )); |
Analysis of the above code
SQL queries using the in method. If there are more articles on top, the more data the $ sticky variable will be, the slower the query speed. At present, the amount of data in the stage is small, and the query will be much slower as there is more data.
The code for optimizing the homepage pin is as follows:
The code is as follows: |
Copy code |
$ Sticky = get_option ('sticky _ posts'); rsort ($ sticky ); If (is_array ($ sticky )){ $ Num = count ($ sticky ); If ($ num> 4 ){ $ Num = 4; } $ Sticky = array_slice ($ sticky, 0, $ num ); } Query_posts (array ('post _ in' => $ sticky, 'Caller _ get_posts' => 1, 'showposts' => 4 )); |
In fact, the principle is to call the get_option ('sticky _ posts') array to four, and then use in to query