Wordpress post homepage top-up implementation example

Source: Internet
Author: User

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

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.