WordPress system to increase the selection of the article sorting way

Source: Internet
Author: User
Tags rand

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

Related Article

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.