wordpress如何实现后台显示文章和分类ID
方法一:
可以直接将下面的代码添加到当前主题的 functions.php 文件即可
<?php
/**
* WordPress后台的文章、分类,媒体,页面,评论,链接等所有信息中显示ID并将ID设置为第一列
*/
// 添加一个新的列 ID
function ssid_column($columns) {
//将ID设置为第一列
return array_merge(array('ssid' => 'ID'), $columns);
}
// 显示 ID
function ssid_value($column_name, $id) {
if ($column_name == 'ssid')
echo $id;
}
function ssid_return_value($value, $column_name, $id) {
if ($column_name == 'ssid')
$value = $id;
return $value;
}
function ssid_css() {
echo "<style>.column-ssid,#ssid{width: 50px!important;text-align: center!important;padding: 0px!important;margin: 0px!important;}</style>";
}
// 通过动作/过滤器输出各种表格和CSS
function ssid_add() {
add_action('admin_head', 'ssid_css');
//文章
add_filter('manage_posts_columns', 'ssid_column');
add_action('manage_posts_custom_column', 'ssid_value', 10, 2);
// 页面
add_filter('manage_pages_columns', 'ssid_column');
add_action('manage_pages_custom_column', 'ssid_value', 10, 2);
// 媒体
add_filter('manage_media_columns', 'ssid_column');
add_action('manage_media_custom_column', 'ssid_value', 10, 2);
// 连接
add_filter('manage_link-manager_columns', 'ssid_column');
add_action('manage_link_custom_column', 'ssid_value', 10, 2);
// 连接分类
add_action('manage_edit-link-categories_columns', 'ssid_column');
add_filter('manage_link_categories_custom_column', 'ssid_return_value', 10, 3);
foreach ( get_taxonomies() as $taxonomy ) {
add_action("manage_edit-${taxonomy}_columns", 'ssid_column');
add_filter("manage_${taxonomy}_custom_column", 'ssid_return_value', 10, 3);
}
// 用户
add_action('manage_users_columns', 'ssid_column');
add_filter('manage_users_custom_column', 'ssid_return_value', 10, 3);
// 评论
add_action('manage_edit-comments_columns', 'ssid_column');
add_action('manage_comments_custom_column', 'ssid_value', 10, 2);
}
add_action('admin_init', 'ssid_add');
?>