Aggiunta la colonna "ordine menu" alla schermata di amministrazione del tipo di post personalizzato
4 risposta
- voti
-
- 2012-03-01
OK - allafine si è rivelato abbastanza semplice - dato che avevo una sorta dibloccomentale -
menu_order
è una variabilenell'oggetto$post
(grazie a @bradyper avermelo ricordato).Il post di @ scribu sulla creazione di valori di colonna ordinabili fornisceil resto.
Quindi,supponendo cheiltipo dipostpersonalizzato si chiami
header_text
,queste sono lefunzionie gli hooknecessari:Aggiungi unanuova colonnaper l'ordine
/** * aggiungi la colonna dell'ordine alla schermata dell'elenco degli amministratoriperiltesto dell'intestazione */ function add_new_header_text_column ($ header_text_columns) { $ header_text_columns ['menu_order']="Ordine"; return $ header_text_columns; } add_action ('manage_header_text_post_columns','add_new_header_text_column'); Renderizzai valori della colonna
/** *mostrai valori della colonna dell'ordinepersonalizzato */ funzione show_order_column ($name) { global $post; switch ($name) { case 'menu_order': $ order=$post- >menu_order; echo $ order; rompere; predefinito: rompere; } } add_action ('manage_header_text_posts_custom_column','show_order_column'); Imposta la colonnain modo che sia ordinabile
/** * Rendi la colonna ordinabile */ funzione order_column_register_sortable ($ colonne) { $ colonne ['menu_order']='menu_order'; restituire $ colonne; } add_filter ('manage_edit-header_text_sortable_columns','order_column_register_sortable'); OK - in the end turned out to be fairly simple - as I'd had some kind of mental block -
menu_order
is a variable in the$post
object (thanks to @brady for reminding me of that).@scribu's post on creating sortable column values then gives the rest.
So, assuming the custom post type is called
header_text
, these are the functions and hooks that are needed: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_post_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');
-
In WP 3.1,manage_edit - $ {post_type} _columns è stato soppiantato damanage _ $ {post_type} _posts_columns: [codex] (http://codex.wordpress.org/Plugin_API/Filter_Reference/manage_edit-post_type_columns)In WP 3.1, manage_edit-${post_type}_columns has been supplanted by manage_${post_type}_posts_columns: [codex](http://codex.wordpress.org/Plugin_API/Filter_Reference/manage_edit-post_type_columns)
- 2
- 2013-05-29
- ptriek
-
Non c'è davvero unmodoper rendere questo l'ordinepredefinito?Tipiacciono lepagine?Is there really just not way to make this the default ordering? Like Pages?
- 0
- 2013-09-03
- Howdy_McGee
-
Aggiornoil codiceperessere conforme a WP 3.1+ come riportato da @codex.I update the code to comply with WP 3.1+ as @codex reported.
- 0
- 2020-01-11
- lepe
-
- 2012-11-23
Èpassatotroppotempo,ma soloper la cronaca,puoi visualizzare l'opzione "menu order"nell'amministratore,semplicementeincludendo "page-attributes"nell'array di opzioni "supports".Adesempio:
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 ) );
It's been too long, but just for the record, you can display the 'menu order' option in the admin, just by including 'page-attributes' in the 'supports' option array. For example:
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 ) );
-
Lapersona chepubblica la domanda ègià a conoscenza degli "attributi dellapagina".Non devemostrareil valore dell'ordine dellepaginenelmodonecessario.The person posting the question is aware of 'page-attributes' already. It must not show the page order value in the way needed.
- 2
- 2012-11-23
- s_ha_dum
-
Ho attributi dipaginanei supportiperiltipo dipostpersonalizzato,che aggiunge l'ordine deimenu allabarra laterale quando simodifica uno di queitipi dipostpersonalizzati,ma quando si visualizza l'elenco dituttiglielementineltipo dipostpersonalizzatoin wp adminnon lofamostra la colonna dell'ordine deimenu.Ènecessario qualcos'altroperfarlo vedere?Sotto le opzioni dello schermoper le colonne la data è l'unica opzionee quella è selezionata.I have page-attributes in the supports for the custom post type, which does add menu order to the sidebar when editing one of those custom post types, but when viewing the list of all the items in the custom post type in wp admin it doesnt show menu order column. Is there something else required to get it to show? Under screen options for columns date is the only option and that is checked.
- 0
- 2019-09-20
- cchiera
-
@cchiera: Devi seguire la risposta accettataper visualizzare la colonna.La semplice aggiunta di "attributi dipagina"non sembrafunzionare.@cchiera: You must follow the accepted answer in order to display the column. Just adding 'page-attributes' doesn't seems to work.
- 0
- 2020-01-11
- lepe
-
- 2020-04-27
@anumi ha lasciatonellagiusta direzione,tuttavia questonon è un codicemoderno
Soluzionemoderna & amp; & amp; lavorando su WordPress 5.4
- aggiungi supporto
- colonna del registro
- visualizzail valore della colonna
- rendi la colonna registrata ordinabile/dìin base a ciò che dovrebbe ordinare
...
$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; });
Se seiinteressato a come creare una colonna ordinabile chenon siabasata su una variabile di query dibase,giocheresti conilfiltro
request
come descrittonella documentazione qui: https://make.wordpress.org/docs/plugin-developer-handbook/10-plugin-components/custom-list-table-columns/# sortable-columns@anu let me to the right direction, however that is not a modern code
Modern solution && working on WordPress 5.4
- add support
- register column
- display column value
- make the registered column sortable / say by what it should sort
...
$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; });
If you are interested how would you make sortable column that is not based on some basic Query variable you would play around
request
filter as described in documentation here: https://make.wordpress.org/docs/plugin-developer-handbook/10-plugin-components/custom-list-table-columns/#sortable-columns -
- 2012-02-29
Devi registrareiltuo CPN con:
'hierachical' => true
You have to register your CPT with:
'hierachical' => true
-
Questononti consente solo di specificare ungenitoreper l'elementonella schermata dimodifica? L'hoprovatoe nonfa differenzaper la schermata dell'elencoDoesn't that just allows you to specify a parent for the item in the edit screen - I've tried it, and it makes no difference to the listing screen
- 0
- 2012-02-29
- anu
-
scusa,non avevo capito che desideri aggiungere colonnepersonalizzate all'elenco ...forse questoti dà unpunto dipartenza: http://shibashake.com/wordpress-theme/add-custom-post-type-columnssorry, didn't realize you want to add custom columns to the list ... maybe this gives you a point to start:http://shibashake.com/wordpress-theme/add-custom-post-type-columns
- 1
- 2012-02-29
- ungestaltbar
Desidero utilizzare l'attributo
menu order
per controllare l'ordinamento di untipo di articolopersonalizzato che verrà utilizzatoper uno scopo specifico.È abbastanzafacile aggiungerlo al CPTtramite
supports => array('page-attributes')
ma comefaccio aesporreil valore dell'ordine delmenunella schermata dell'elenco di amministrazioneper questo CPT?