Solution to the conflict between query_posts function and WP-pagenavi plug-in WordPress

Source: Internet
Author: User
Tags php template

Recently received a requirement: display on the home pageArticleThe content and category directory page must display the article title in the form of a list. This requirement is actually very simple. You only need to remove the_content () or the_excerpt () from the archive. php template file to display the article title list. However, the BT requirement is that only one article is displayed in the homepage log, that is, the number of articles in the background is set to 1, but the title list of the category directory cannot be 1, as a result, this becomes complicated. Changed to: the number of logs on the home page is 1, but the number of logs in the list of classified directories is not 1, which is 10 or 20. This seems to be more harmonious.

So I was about to find the plug-in, but I searched for a plug-in that basically didn't have any available plug-ins, so I decided to change the template myself.CodeYou only need to set the number of logs on the home page or the number of logs on the article list page to the number you want, and work with the number of articles in the background to achieve this function. After a query, the query_posts () function can be used to change the number of $ posts. Very excited. In principle, you only need to specify the number of showposts in the query_posts () function, for example, query_posts ("showposts = 3"). Add this code to have_posts () you can specify the number of articles.

However, the derivative problem occurs at this time: the WP-pagenavi plug-in cannot obtain the correct paging result. It is often reported that the 400 error has not been found. Certainly: The query_posts function conflicts with the WP-pagenavi plug-in. How can this problem be solved? I still want to specify showposts from the document directory list. Can I specify cat? Or, as I said on the internet, determine the current page? $ Paged = (get_query_var ('page '))? Get_query_var ('page'): 1;

All tests fail. It was very popular, so it was almost impossible to search for Google again, but almost no one encountered this problem in China. I found that a foreigner had solved this problem. The specific function code is as follows:

Function curpageurl (){
$ Pageurl = 'http ';
// Check what if its secure or not
If ($ _ server ["HTTPS"] = "on "){
$ Pageurl. = "S ";
}
// Add the Protocol
$ Pageurl. = "://";
// Check what port we are on
If ($ _ server ["server_port"]! = "80 "){
$ Pageurl. = $ _ server ["SERVER_NAME"]. ":". $ _ server ["server_port"]. $ _ server ["request_uri"];
} Else {
$ Pageurl. = $ _ server ["SERVER_NAME"]. $ _ server ["request_uri"];
}
// Cut off everything on the URL except t the last 3 Characters
$ Urlend = substr ($ pageurl,-3 );
// Strip off the two forward shashes
$ Page = str_replace ("/", "", $ urlend );
// Return just the number
Return $ page;

However, it seems that this code is not omnipotent, and there is still a problem with paging under the category directory. It seems a little desperate. Let me change my mind: I used to modify the category directory template to get a list of the correct number of articles. Now I will modify the homepage template and set the number of articles to 1, at the same time, the number of articles in the category directory is still controlled by the background, which is more flexible. In principle, the article list page does not conflict with the WP-pagenavi plug-in (after all, query_posts function is not used ). After testing, I was very excited and found that the test on the home page was okay and I could specify any number, but I still don't know why. Okay, I will not go into details.

The last small problem encountered was that the page section could not be correctly displayed. The solution is to add a restriction before these query_posts. You can use the is_home () function to determine whether the homepage is used. All the codes are as follows:

If (is_home ()){
Function curpageurl (){
$ Pageurl = 'http ';
// Check what if its secure or not
If ($ _ server ["HTTPS"] = "on "){
$ Pageurl. = "S ";
}
// Add the Protocol
$ Pageurl. = "://";
// Check what port we are on
If ($ _ server ["server_port"]! = "80 "){
$ Pageurl. = $ _ server ["SERVER_NAME"]. ":". $ _ server ["server_port"]. $ _ server ["request_uri"];
} Else {
$ Pageurl. = $ _ server ["SERVER_NAME"]. $ _ server ["request_uri"];
}
// Cut off everything on the URL except t the last 3 Characters
$ Urlend = substr ($ pageurl,-3 );
// Strip off the two forward shashes
$ Page = str_replace ("/", "", $ urlend );
// Return just the number
Return $ page;
}

$ Paged = (get_query_var ('page '))? Get_query_var ('page'): 1;
$ ARGs = array (
// Add the query_posts parameter as follows. For specific parameters, see the official documentation.
'Showposts' => 10,
'Paged' => curpageurl ()
);
Query_posts ($ ARGs );
}

The preceding content can be placed before your if (have_posts. If you like it, you can use it. I don't know if you have such a Bt demand? If you have any questions, please contact us. Haha

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.