wordpress后台所有文章列表显示缩略图


wordpress在默认情况下后台的文章列表是没有缩略图这个选项的,如果我们要查看文章是否有缩略图只能点击文章详情页去查看,那有没办法让我们能在后台的文章列表中就可以看到文章是否有缩略图呢?答案是当然可以。我们先来看下最终实现的列表的效果图:
sltlb
下面我们就来看下wordpress后台文章列表如何显示缩略图
打开你主题的functions.php文件添加如下代码:

  1. if ( !function_exists('fb_AddThumbColumn') && function_exists('add_theme_support') ) {
  2. // 在文章列表页与页面列表页添加缩略图列表
  3. add_theme_support('post-thumbnails', array( 'post', 'page' ) );
  4. function fb_AddThumbColumn($cols) {
  5. $cols['thumbnail'] = __('Thumbnail');
  6. return $cols;
  7. }
  8. function fb_AddThumbValue($column_name, $post_id) {
  9. $width = (int) 35;
  10. $height = (int) 35;
  11. if ( 'thumbnail' == $column_name ) {
  12. // thumbnail of WP 2.9
  13. $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
  14. // image from gallery
  15. $attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );
  16. if ($thumbnail_id)
  17. $thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
  18. elseif ($attachments) {
  19. foreach ( $attachments as $attachment_id => $attachment ) {
  20. $thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
  21. }
  22. }
  23. if ( isset($thumb) && $thumb ) {
  24. echo $thumb;
  25. } else {
  26. echo __('None');
  27. }
  28. }
  29. }
  30. // 文章页调用
  31. add_filter( 'manage_posts_columns', 'fb_AddThumbColumn' );
  32. add_action( 'manage_posts_custom_column', 'fb_AddThumbValue', 10, 2 );
  33. // 页面调用
  34. add_filter( 'manage_pages_columns', 'fb_AddThumbColumn' );
  35. add_action( 'manage_pages_custom_column', 'fb_AddThumbValue', 10, 2 );
  36. }