在 WordPress 后台文章管理列表显示特色图像(Featured Image)

2016-03-01 06:10 1,001 4 条评论 龙笑天下
Dragon主题购买

特色图像(featured image)是 WordPress 3.0+ 的一个不错的功能,允许你给文章设置一个图片作为它的缩略图显示。在后台,为了直观地了解每篇文章的特色图像是什么,建议在 WordPress 后台文章管理列表显示每篇文章的特色图像,如下图所示:

在 WordPress 后台文章管理列表显示特色图像(Featured Image)

实现的方法很简单,有 2 种。

第一种:用插件

使用 Featured Image Column 插件即可。在后台插件安装界面搜索 Featured Image Column 即可在线安装,或者下载Featured Image Column

启用该插件后,你就会发现在后台文章管理列表多出了一栏显示特色图像。

如果你希望没有特色图像时在后台文章列表显示默认的图像,可以在主题的 functions.php 中添加下面的代码:

 	function my_custom_featured_image_column_image( $image ) {
        if ( !has_post_thumbnail() )
            return trailingslashit( get_stylesheet_directory_uri() ) . 'images/featured-image.png';
    }
    add_filter( 'featured_image_column_default_image', 'my_custom_featured_image_column_image' );

然后将 名为 featured-image.png 的图片上传到当前主题下的 images 文件夹里。

第二种:用代码

在 functions.php 中加入以下代码即可:

/**
* WordPress 后台文章列表后面显示特色图像 By ILXTX.COM
* https://www.ilxtx.com/wordpress-featured-image-column.html
*/
if (function_exists( 'add_theme_support' )){
    add_filter('manage_posts_columns', 'my_add_posts_columns', 5);
    add_action('manage_posts_custom_column', 'my_custom_posts_columns', 5, 2);
}
function my_add_posts_columns($defaults){
   $defaults['my_post_thumbs'] = '特色图像';
    return $defaults;
}
function my_custom_posts_columns($column_name, $id){
    if($column_name === 'my_post_thumbs'){
        echo the_post_thumbnail( array(125,80) );
    }
}

可以调整其中的 125,80,改变图像的大小。

针对代码版,知言tinection主题的改进

在没有设置特色图像时,自动获取文章的第一张图像。代码如下:

/**
* WordPress 后台文章列表后面显示特色图像(知言 tinection 主题专用) By ILXTX.COM
* https://www.ilxtx.com/featured-image-column.html
*/
if (function_exists( 'add_theme_support' )){
    add_filter('manage_posts_columns', 'my_add_posts_columns', 5);
    add_action('manage_posts_custom_column', 'my_custom_posts_columns', 5, 2);
}
function my_add_posts_columns($defaults){
   $defaults['my_post_thumbs'] = '特色图像';
    return $defaults;
}
function my_custom_posts_columns($column_name, $id){
    if($column_name === 'my_post_thumbs'){
    	if(has_post_thumbnail()){
        echo the_post_thumbnail( array(125,80) );}
        else echo '<img src="'.tin_thumb_source(catch_first_image(),125,80,false).'" />';
    }
}

可以调整其中的 125,80,改变图像的大小。

「点点赞赏,手留余香」

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

2016-01-18

2016-03-08

发表评论

表情 格式 贴图 链接 私密 签到
Dragon主题购买阿里云特价云服务器1核2G低至86元,N4共享型服务器3年仅需799元腾讯云特价云服务器1核2G 88元/年 2核4G3M688元/3年,更有千元代金券礼包免费领!
评论
正在努力加载中...
扫一扫二维码分享
×
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies. Learn more