Sort and summarize WordPress articles by the last update time

Source: Internet
Author: User
Tags echo date


The default WordPress article display is sorted by posting time. Sometimes, the blogger needs to make some modifications to the previous article, in this way, you need to display the modified article to the first position, and the article needs to be sorted by the modification time.

The modification method is also very simple, but this time it is not to add code to the functions. php file, but to modify it in the index. php file on the homepage of your topic.

There are several situations that need to be considered. The first one is not set to top. This kind of scenario is relatively simple. You can simply add a line of code to implement it, and the other one is set to top articles, which need to be filtered out, the operation is as follows:

I. No top posts

You can find it in index. php of your topic:

<? Php if (have_posts (): while (have_posts (): the_post ();?>

Add the following code before the above code:

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

Now, refresh the homepage and you will find that the articles are sorted by the last update order, but the time is still displayed as the article release time. Then you need to modify the article time to display it, replace the original time display code with the following:

Last Updated: <? Php the_modified_time ('Y-m-D');?>

In this way, you can edit the archive. php file in the same way as on other classification pages.

2. Post display

The second category is post Display. Just like me, my homepage uses slide display for top posts. Therefore, you need to filter out these articles, if your structure is the same as mine, you can use the original index. php has found code similar to the following:

<? Php
$ Sticky = get_option ('sticky _ posts ');
$ Args = array (
'Cat' => '-906 ',
'Ignore _ sticky_posts' => 1,
'Post _ not_in '=> $ sticky,
'Paged' => $ paged
);
Query_posts ($ args );
?>
You need to add a parameter here:

'Orderby' => 'modified'
Complete:

<? Php
$ Sticky = get_option ('sticky _ posts ');
$ Args = array (
'Cat' => '-906 ',
'Ignore _ sticky_posts' => 1,
'Post _ not_in '=> $ sticky,
'Paged' => $ paged,
'Orderby' => 'modified'
);
Query_posts ($ args );
?>
The time display is the same as the one in the preceding figure.

Wordpress homepage articles are sorted by update time

Before the following code of index. php

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

Add

<? Php $ posts = query_posts ($ query_string. '& orderby = modified');?>

You can.

Display last modified time

Insert the following code into a proper location:

<Div> last updated: <? Php the_modified_time ('Y-m-D');?> </Div>

Display the last modification time separately:

<? Php $ u_time = get_the_modified_time ('u ');

Echo date ("y-m", $ u_time );

Echo date ("d", $ u_time );

Echo date ("D", $ u_time );

?>


The wordpress background article list is sorted by the last modified time.

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


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 ');

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.