順序に入力した数字順に投稿を表示させる方法
function.php
function add_posts_columns($columns) {
$columns['menu_order'] = '順序';
return $columns;
}
function add_posts_columns_row($column_name, $post_id) {
if ( 'menu_order' == $column_name ) {
$menu_order = get_post($post_id) -> menu_order;
echo $menu_order;
}
}
add_filter( 'manage_posts_columns', 'add_posts_columns' );
add_action( 'manage_posts_custom_column', 'add_posts_columns_row', 10, 2 );
投稿以外で順序を使う場合はピンクを指定のpost_typeにする(管理画面URLから判断)
add_post_type_support( 'post', 'page-attributes' );
function make_order_column_sortable( $columns ) {
$columns['menu_order'] = 'menu_order';
return $columns;
}
add_filter( 'manage_edit-psot_sortable_columns', 'make_order_column_sortable' );
function cate_change_sort_order( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if($query->is_category('news') || $query->is_category('feature') ){
}else if ( $query->is_category() ) {
$query->set( 'order', 'ASC' );
$query->set( 'orderby', 'menu_order' );
}
}
add_action( 'pre_get_posts', 'cate_change_sort_order' );