There have been a lot of netizens asked me, Lulu blog Home page Visitors can choose their own way of sorting the effect of the article is how to do, because before the work is relatively busy, I am sorry did not give these netizens timely reply. Today to share this article sorting effect of the implementation process.
In fact, the implementation process is also relatively simple, one is to construct the link, the other is to use query_posts to change the main loop on it.
Constructing links
Links are mainly used to pass get parameters and let the PHP program know exactly how you want to sort. In the index.php of the subject you need to insert the following code, used to output the Sort button HTML, the Sort button style, you write your own CSS again. Note that the following code automatically gets the sort method that the current user has selected and adds class= to the Sort button's Li
code is as follows |
copy code |
<ul> <li>< A <?php if (isset ($_get[' order ')) && ($_get[' order ']== ' rand ')) echo ' class= ' the current ';?> href= '/?order=r and "rel=" nofollow "> Random reading </a></li> <li><a <?php if isset ($_get[' Order '] && ($_get[' order ']== ' commented ')) echo ' class= ' the current ';?> href= '/?order=commented ' rel= ' Nofollow "> Comments max </a></li> <li><a <?php if isset ($_get[' order ') && ($_get[' order ']== ' alpha ')] echo ' class= ' current ';?> href= '/?order=alpha ' rel= ' nofollow ' > title sort </ A></li> </ul> |
Change the main loop
First you have to find the following statement in the index.php of the topic:
The code is as follows |
Copy Code |
if (Have_posts ()) |
Then add the following code before this sentence:
The code is as follows |
Copy Code |
if (Isset ($_get[' order ')) { Switch ($_get[' order ') { Case ' rand ': $orderby = ' Rand '; Break Case ' commented ': $orderby = ' comment_count '; Break Case ' alpha ': $orderby = ' title '; Break Default: $orderby = ' title '; } Global $WP _query; $args = Array (' By ' => $orderby, ' order ' => ' DESC '); $arms = Array_merge ($args, $wp _query->query); Query_posts ($arms); } if (Have_posts ()) |
All right, that's it. Copy and paste, easy to achieve sorting effect