Home > CMS Tutorial > WordPress > body text

A one-minute introduction to how to add sorting options to WordPress articles

藏色散人
Release: 2021-08-17 15:15:56
forward
3711 people have browsed it

The following tutorial column from WordPress will introduce how to add sorting options to WordPress articles. I hope it will be helpful to friends in need!

A one-minute introduction to how to add sorting options to WordPress articles

#By default, WordPress only has a sorting options panel for pages. You can use the following code to make articles also have a sorting options panel function.

Add the code to the current theme function template functions.php.

add_action( 'admin_init', 'posts_order' );
function posts_order() {
    add_post_type_support( 'post', 'page-attributes' );
}
Copy after login

If the front-end wants to implement the article sorting function, it needs to add parameters in the main loop:

$order_posts = new WP_Query(array(
    'post_type' => 'post', 
    'post_status' => 'publish', 
    'orderby' => 'menu_order', 
    'order' => 'ASC', 
) );
Copy after login

The background article list displays the sorting number:

add_action('manage_posts_custom_column',  'zm_posts_order_show_columns');
function zm_posts_order_show_columns($name) {
global $post;
switch ($name) {
case 'order':
$views = $post->menu_order;
echo $views;
break;
}
}
 
add_filter('manage_posts_columns', 'zm_posts_order_columns');
function zm_posts_order_columns($defaults) {
$defaults['order'] = '排序';
return $defaults;
}
Copy after login

Batch clearing sorting number:

global $wpdb;
$wpdb->query("UPDATE wp_posts SET menu_order = 0 WHERE post_type = 'post'");
Copy after login

The above is the detailed content of A one-minute introduction to how to add sorting options to WordPress articles. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:zmingcx.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!