在 WordPress 中,每个分类、文章、页面都有一个唯一的 ID 。在使用或者开发主题的时候,可能有需要获取相对应的文章或者页面 ID。例如在展示特色文章幻灯片,你需要获取对应文章的 id 等等。
现在教你一个方法快速在后台文章和页面列表显示对应的 ID 的办法。
将下面的代码添加到主题的 functions.php 文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //WordPress后台文章列表、页面列表中显示对应ID function fanly_post_id_column( $post_columns ) { $beginning = array_slice( $post_columns, 0 ,1 ); $beginning[ 'postid' ] = __( 'ID', 'fanly' ); $ending = array_slice( $post_columns, 1 ); $post_columns = array_merge( $beginning, $ending ); return $post_columns; } add_filter( 'manage_posts_columns', 'fanly_post_id_column' );//添加文章列表页ID标题 add_filter( 'manage_pages_columns', 'fanly_post_id_column' );//添加页面列表页.... function fanly_posts_id_column( $col, $val ) { if( $col == 'postid' ) echo $val; } add_action( 'manage_posts_custom_column', 'fanly_posts_id_column', 10, 2 );//添加文章列表页ID列数值 add_action( 'manage_pages_custom_column', 'fanly_posts_id_column', 10, 2 );//添加页面列表页ID... function fanly_posts_id_column_css() { echo '<style type="text/css">#postid { width: 50px; }</style>';//ID列宽度 } add_action( 'admin_head-edit.php', 'fanly_posts_id_column_css' ); |