WordPress manually changes the order of articles to get rid of the ascending and descending order of publishing time

Time:2019-11-29

Our common WordPress site article sorting is usually arranged in ascending or descending order of publishing time, which can almost meet the needs of most sites, but some other sites may have some other needs: I want to manually change the arrangement of articles, and you can change the arrangement of articles at will. In fact, WordPress’s query posts can easily solve this problem, but you have to change some code yourself.

Modification method
1. Open the current theme directory of WordPress, open index.php, and search code:

Copy code

The code is as follows:

while (have_posts())

Replace it with:

Copy code

The code is as follows:

/**
*Name: WordPress to manually change the order of articles
*Last revised: November 14, 2010
*/
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$args=array(
‘meta_key’ => ‘sort’,
‘orderby’ = > meta_value_num ‘, / / WordPress version 2.8 and above is required
‘paged’ => $paged,
‘order’ => DESC
);
query_posts($args);
while (have_posts())

Then search:

Copy code

The code is as follows:

endwhile;

Change it to:

Copy code

The code is as follows:

endwhile;wp_reset_query();

2. Add a custom field (or a custom column, which can be seen at the bottom of the article editing page) to all previously published articles. The name is sort, and the value is filled in numbers. In this way, on the home page, articles are arranged according to the value of the sort field, from large to small. By modifying the value of the sort field, you can customize the order of articles.
3. It should be noted that after the above modifications, you must add the custom field sort to all articles, otherwise articles without the field sort will not be displayed on the home page. This may be the price of manual sorting, or what is manual?
4. If you want to manually modify the order of the articles in the archive pages, such as tabs and classification pages, you can also modify the archive.php under the topic directory according to the method described in the first step.