向自定义POST类型管理屏幕添加‘Menu Order’栏

时间:2012-02-29 作者:anu

我想使用menu order 属性来控制将用于特定目的的自定义帖子类型的顺序。

很容易通过以下方式将其添加到CPTsupports => array(\'page-attributes\') 但是如何在这个CPT的管理列表屏幕上公开菜单顺序值呢?

4 个回复
最合适的回答,由SO网友:anu 整理而成

好吧-结果证明很简单-因为我有某种心理障碍-menu_order 是中的变量$post 对象(感谢@brady提醒我这一点)。

@斯克里布的post on creating sortable column values 然后给出其余部分。

因此,假设调用了自定义post类型header_text, 以下是所需的功能和挂钩:

Add a new column for the order

/**
* add order column to admin listing screen for header text
*/
function add_new_header_text_column($header_text_columns) {
  $header_text_columns[\'menu_order\'] = "Order";
  return $header_text_columns;
}
add_action(\'manage_header_text_posts_columns\', \'add_new_header_text_column\');

Render the column values

/**
* show custom order column values
*/
function show_order_column($name){
  global $post;

  switch ($name) {
    case \'menu_order\':
      $order = $post->menu_order;
      echo $order;
      break;
   default:
      break;
   }
}
add_action(\'manage_header_text_posts_custom_column\',\'show_order_column\');

Set the column to be sortable

/**
* make column sortable
*/
function order_column_register_sortable($columns){
  $columns[\'menu_order\'] = \'menu_order\';
  return $columns;
}
add_filter(\'manage_edit-header_text_sortable_columns\',\'order_column_register_sortable\');

SO网友:Juan Salas

时间太长了,但为了便于记录,您可以在管理中显示“菜单顺序”选项,只需在“支持”选项数组中包含“页面属性”。例如:

    register_post_type( \'columna\',
    array(
        \'labels\' => array(
            \'name\' => __( \'Columnas\' ),
            \'singular_name\' => __( \'Columna\' ),
        ),
        \'supports\' => array( \'title\', \'thumbnail\', \'excerpt\', \'page-attributes\' ),
        \'public\' => true,
        \'has_archive\' => false,
        \'menu_position\'=>5
    )
);

SO网友:jave.web

@anu让我找到了正确的方向,但这不是一个现代代码

现代解决方案&;正在处理WordPress 5.4

  1. 添加支持
  2. 注册列
  3. 显示列值
  4. 使注册列可排序/按其应排序的内容排序
    1. 。。。

      $MY_POST_TYPE = "flowers"; // just for a showcase
      
      // the basic support (menu_order is included in the page-attributes)
      add_post_type_support($MY_POST_TYPE, \'page-attributes\');
      
      // add a column to the post type\'s admin
      // basically registers the column and sets it\'s title
      add_filter(\'manage_\' . $MY_POST_TYPE . \'_posts_columns\', function ($columns) {
        $columns[\'menu_order\'] = "Order"; //column key => title
        return $columns;
      });
      
      // display the column value
      add_action( \'manage_\' . $MY_POST_TYPE . \'_posts_custom_column\', function ($column_name, $post_id){
        if ($column_name == \'menu_order\') {
           echo get_post($post_id)->menu_order;
        }
      }, 10, 2); // priority, number of args - MANDATORY HERE! 
      
      // make it sortable
      $menu_order_sortable_on_screen = \'edit-\' . $MY_POST_TYPE; // screen name of LIST page of posts
      add_filter(\'manage_\' . $menu_order_sortable_on_screen . \'_sortable_columns\', function ($columns){
        // column key => Query variable
        // menu_order is in Query by default so we can just set it
        $columns[\'menu_order\'] = \'menu_order\';
        return $columns;
      });
      
      如果您感兴趣,您将如何使不基于某个基本查询变量的可排序列成为您可以使用的列request 按此处文档所述进行筛选:https://make.wordpress.org/docs/plugin-developer-handbook/10-plugin-components/custom-list-table-columns/#sortable-columns

SO网友:ungestaltbar

您必须向以下机构注册您的CPT:

\'hierachical\' => true

结束

相关推荐

wp_query order by title

我有以价格开头的帖子标题,例如:199900美元-123街。我正在使用下面的代码,我无法通过标题升序来正常工作。请提供任何建议。<?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; $args = array( \'cat\' =>21, \'posts_per_page\' => 999999, \'paged\' => $paged,&#x