How to sort wordpress articles without changing the document order

Source: Internet
Author: User

Unordered articles

The code for query_posts is as follows:

The code is as follows: Copy code

<? Php
$ Display_categories = get_option ('swt _ cat_exclude ');
$ Limit = get_option ('posts _ per_page ');
$ Paged = (get_query_var ('paged '))? Get_query_var ('paged'): 1;
$ Sticky = get_option ('sticky _ posts ');
$ Args = array (
'Cat' =-> $ display_categories,
'Showposts' => $ limit = 11,
// Ignore sticky_posts, that is, do not set the top (do not mention the top article to the top), but output the top article
'Ignore _ sticky_posts' => 1,
// Exclude top posts and do not output top posts. Only one sentence can be left for this sentence and the previous one, which can be handled as needed.
'Post _ not_in '=> $ sticky,
'Paged' => $ paged
);
Query_posts ($ args );
If (have_posts ()):
While (have_posts (): the_post ();
/* Add output content here, such as title, date, author, and abstract */
Endwhile;
Endif;
?>

This code is a method for processing and filtering top posts found on the internet. However, the search, tag, and category pages mentioned earlier may only display the content of the home page. To solve this problem, the query conditions such as s and tag are added to the array. Although tabs and search pages can be queried normally, the classification page may be faulty. Therefore, you can only find another method.

After carefully reading the wordpress method instructions, we found that the query_posts method can be configured with the global variable $ query_string, without affecting other query results. The code is as follows:

The code is as follows: Copy code

Global $ query_string;

Query_posts ($ query_string. '& ignore_sticky_posts = 1 & prime ;);


Set Display/do not show top posts

Under the default topic, if you set an article to the top of the topic, the article will be taken to the beginning of the article list on the homepage. At this time, you can add a pin icon or the word "[pin to top]" as the difference. Simply add a code near the code in the title of the article, and use the is_sticky () function to determine whether it is the top article. If so, add the corresponding icon or text. The code is as follows:

The code is as follows: Copy code

<? Php if (is_sticky () {echo '';}?>

If the requirement is not high, this will be done and the goal has been achieved. However, if you have high requirements, you want to separate the top article from the normal article, and want to make the top article and the general article have a significant difference, for example, the top article only displays the title, the following method can be used to display more content in common articles.

First, make a slight adjustment to the code of all the original articles, so that they do not bring the top article to the beginning, but still appear in the article list. Replace the following code with the code of all original output articles. Generally? Php if (have_posts (): while (have_posts (): the_post ();?> Before. Note: The following code partially overlaps with the original code. You can add different key codes to the corresponding positions, mainly the lines with comments. The code is as follows:

The code is as follows: Copy code

<? Php
$ Display_categories = get_option ('swt _ cat_exclude ');
$ Limit = get_option ('posts _ per_page ');
$ Paged = (get_query_var ('paged '))? Get_query_var ('paged'): 1;
$ Sticky = get_option ('sticky _ posts ');
$ Args = array (
'Cat' = --> $ display_categories,
'Showposts' => $ limit = 11,
// Ignore sticky_posts, that is, do not set the top (do not mention the top article to the top), but output the top article
'Ignore _ sticky_posts' => 1,
// Exclude top posts and do not output top posts. Only one sentence can be left for this sentence and the previous one, which can be handled as needed.
'Post _ not_in '=> $ sticky,
'Paged' => $ paged
);
Query_posts ($ args );
If (have_posts ()):
While (have_posts (): the_post ();
/* Add output content here, such as title, date, author, and abstract */
Endwhile;
Endif;
?>


Then, add the code that only displays the top article in the appropriate position. In general, it is placed in the front, so as to reflect the top effect, that is, before displaying the code of all the article lists. The code is as follows:

The code is as follows: Copy code

<? Php
// Obtain all top posts
$ Sticky = get_option ('sticky _ posts ');
// Sort these articles, with the latest date at the top
Rsort ($ sticky );
// Obtain 5 articles
$ Sticky = array_slice ($ sticky, 0, 5 );
// Output these articles
Query_posts (array ('post _ in' = --> $ sticky, 'Ignore _ sticky_posts' => 1 ));
If (have_posts ()):
While (have_posts (): the_post ();
/* Add output content, such as the title */
Endwhile;
Endif;
// This sentence is very important. If it is not added, it may cause some errors, such as page turning effect loss.
Wp_reset_query ();
?>


In this way, the function of displaying and not displaying top posts is basically implemented.

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.