WordPress 在文章列表页增加缩略图显示栏

  1. 首页
  2. 笔记
  3. WordPress 在文章列表页增加缩略图显示栏
301 次阅读
笔记

编辑文章的时候经常忘记配置缩略图,虽然也有很多插件可以自动生成缩略图。

  • 获得文章中第一张图片作为缩略图
  • 当文章未设置缩略图时,自动设置默认缩略图

但是将缩略图显示为文章/页面列表页的一栏显示它不香吗?

将以下代码加入到wp-config.php文件的末尾!

PS:为啥是末尾?万一加上代码之后跟主题冲突了咋办?你可以删掉啊。

为啥不是加到主题的function.php文件中?万一你心血来潮想换个主题不是又得折腾一遍?

/* 以下代码将在文章列表中增加特色图片显示列*/

if ( !function_exists('fb_AddThumbColumn') && function_exists('add_theme_support') ) {
     // for post and page
     add_theme_support('post-thumbnails', array( 'post', 'page' ) );
     function fb_AddThumbColumn($cols) {
         $cols['thumbnail'] = __('Thumbnail');
         return $cols;
     }
     function fb_AddThumbValue($column_name, $post_id) {
         $width = (int) 35;
         $height = (int) 35;
         if ( 'thumbnail' == $column_name ) {
         // thumbnail of WP 2.9
             $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
         // image from gallery
             $attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );
             if ($thumbnail_id)
                 $thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
             elseif ($attachments) {
                 foreach ( $attachments as $attachment_id => $attachment ) {
                     $thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
                 }
             }
            if ( isset($thumb) && $thumb ) {
                echo $thumb;
            } else {
                echo __('None');
            }
        }
    }
    // for posts
    add_filter( 'manage_posts_columns', 'fb_AddThumbColumn' );
    add_action( 'manage_posts_custom_column', 'fb_AddThumbValue', 5, 2 );
    // for pages
    add_filter( 'manage_pages_columns', 'fb_AddThumbColumn' );
    add_action( 'manage_pages_custom_column', 'fb_AddThumbValue', 5, 2 );
}

 

上一篇文章
WordPress 旧文章评论转移到新文章的方法
下一篇文章
英特吉医疗设备官方网站-又一个WordPress网站上线啦!

相关阅读

要发表评论,您必须先登录

最新文章

菜单