我在管理类别管理页面中添加了一个自定义列。
代码是广义的,因为在我的例子中更具体,但含义是相同的
function mytheme_custom_column( $columns )
{
$columns[\'my_column\'] = \'My custom column\';
return $columns;
}
add_filter( \'manage_edit-category_columns\' , \'mytheme_custom_column\' );
function mytheme_custom_column_fill( $content, $column_name, $term_id )
{
if ( \'my_column\' == $column_name ) {
// Get content using $term_id
$content = mytheme_get_custom_field( $term_id );
if( empty( $content ) )
{
// If column is empty, put a minus
$content = \'-\';
}
}
return $content;
}
add_filter( \'manage_category_custom_column\', \'mytheme_custom_column_fill\', 10, 3 );
然后,我在类别创建表单中添加了一个新的自定义字段,并以这种方式保存它。/*
* Saves category custom field on category create
*
*/
function mytheme_save_custom_field( $term_id ){
if( isset( $_POST[\'my_custom_field\'] ) ){
mytheme_save_custom_field( $_POST[\'my_custom_field\'], $term_id );
}
}
add_action( \'created_category\', \'mytheme_save_custom_field\');
创建类别时,Wordpress使用Ajax发送表单,并自动将新的类别行添加到表中。自定义字段应在创建后显示在表中,但始终打印一个“-”,如“自定义字段为空”,但当我重新加载页面时,字段显示正确。
我认为问题在于这个钩子,它在发送ajax响应后触发,但不确定。
add_action( \'created_category\', \'mytheme_save_custom_field\');