WordPress supports sorting of front-end/back-end article lists by update time

Source: Internet
Author: User
Tags wordpress blog

Background articles are sorted by modification time

Add the following php code to functions. php of the current topic:

 

The code is as follows: Copy code

Function ludou_set_post_order_in_admin ($ wp_query ){
If (is_admin ()){
$ Wp_query-> set ('orderby', 'modified ');

// Add the latest modified article to the beginning
// If you want to post the latest modified article, change DESC to ASC.
$ Wp_query-> set ('order', 'desc ');
  }
}
Add_filter ('pre _ get_posts ', 'ludou _ set_post_order_in_admin ');


Actually, I am adding a function. This function is to add modified to the sorting field, and modified is the last update time in this wordpress blog.

Foreground articles are sorted by modification time

1. Open the current WordPress topic directory, Open index. php, and search for the code:

The code is as follows: Copy code

<? Php
While (have_posts (): the_post ();?>

2. Insert the following lines of code before the while

The code is as follows: Copy code

$ Paged = (get_query_var ('paged '))? Get_query_var ('paged'): 1;
$ Args = array (
'Orderby' => modified,
'Showposts' => 10,
'Paged' => $ paged
);
Query_posts ($ args );

3. Continue searching

The code is as follows: Copy code
<? Php endwhile;?>

4. Replace it

The code is as follows: Copy code

<? Php endwhile; wp_reset_query () ;?>


1. According to the orderby value, the document can be sorted in many ways. Below are several common values and their corresponding sorting methods:

Title: Press the title;
Date: based on the release date;
Modified: by the modification time;
ID: by document ID;
Rand: random sorting;
Comment_count: number of comments

2. If you want to modify the order of articles on the tabs, category pages, and other archive pages at the same time, you can also modify archive. php under the topic directory according to the method above.

Save, refresh, success!

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.