默认WordPress仅页面有排序选项面板,可以通过下面的代码让文章也有排序选项面板功能。
将代码添加到当前主题函数模板functions.php中即可。
add_action( 'admin_init', 'posts_order' ); function posts_order() { add_post_type_support( 'post', 'page-attributes' ); }
前端想实现文章排序功能,需要在主循环中添加参数:
$order_posts = new WP_Query(array( 'post_type' => 'post', 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC', ) );
后台文章列表显示排序编号:
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; }
批量清零排序编号:
global $wpdb; $wpdb->query("UPDATE wp_posts SET menu_order = 0 WHERE post_type = 'post'");