Implement the WordPress topic paging function

Source: Internet
Author: User
Tags php template
WordPress's default page flip function is very weak, so page flip is difficult for you, so like wp-pagenavi page flip plug-ins, it is almost a required plug-in for WordPress blogs and a plug-in with a high penetration rate

WordPress's default page flip function is very weak. page flip makes you feel helpless, so like the wp-pagenavi page flip plug-in, it is almost a required plug-in for WordPress blogs and a plug-in with a high penetration rate.

Although there is a tutorial on how to integrate the wp-pagenavi plug-in into the WordPress topic on the Internet, the function code of the wp-pagenavi plug-in is delayed, but the plug-in is only enabled in disguise and is not truly isolated from the wp-pagenavi plug-in. The following lightweight function, coupled with a few lines of css style, can completely replace the paging plug-in.

Implementation method:

First, add the following function code to the topic's functions. php template file:

Function pagination ($ query_string) {global $ posts_per_page, $ paged; $ my_query = new WP_Query ($ query_string. "& posts_per_page =-1"); $ total_posts = $ my_query-> post_count; if (empty ($ paged) $ paged = 1; $ prev = $ paged-1; $ next = $ paged + 1; $ range = 2; // only edit this if you want to show more page-links $ showitems = ($ range * 2) + 1; $ pages = ceil ($ total_posts/$ posts_per_page); if (1! = $ Pages) {echo ""; echo ($ paged> 2 & $ paged + $ range + 1> $ pages & $ showitems <$ pages )? "Top": ""; echo ($ paged> 1 & $ showitems <$ pages )? "Previous Page": ""; for ($ I = 1; $ I <= $ pages; $ I ++) {if (1! = $ Pages &&(! ($ I >=$ paged + $ range + 1 | $ I <= $ paged-$ range-1) | $ pages <= $ showitems )) {echo ($ paged = $ I )? "". $ I. "": "". $ I. "" ;}} echo ($ paged <$ pages & $ showitems <$ pages )? "Next page": ""; echo ($ paged <$ pages-1 & $ paged + $ range-1 <$ pages & $ showitems <$ pages )? "Last": ""; echo "\ n ";}}

Next, replace the default paging code or paging plug-in code in the original template with the following code:

 

Finally, copy the following sample code to style.css and refresh the page, which is the same as page turning using the plug-in.

.pagination{  line-height:23px;  }  .pagination span, .pagination a{  font-size:12px;  margin: 2px 6px 2px 0;  background:#fff;  border:1px solid #e5e5e5;  color:#787878;  padding:2px 5px 2px 5px;  text-decoration:none;  }  .pagination a:hover{  background: #8391A7;  border:1px solid #fff;  color:#fff;  }  .pagination .current{  background: #fff;  border:1px solid #8d8d8d;  color:#393939;  font-size:12px;  padding:2px 5px 2px 5px;  }

The above style is my current topic style. you can modify the border and background based on different themes, and then delete the paging plug-in.

 

 

 

 

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.