Home >CMS Tutorial >WordPress >A one-minute introduction to how to add sorting options to WordPress articles

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

藏色散人
藏色散人forward
2021-08-17 15:15:423789browse

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

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

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;
}

Batch clearing sorting number:

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

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!

Statement:
This article is reproduced at:zmingcx.com. If there is any infringement, please contact admin@php.cn delete