如何在WordPress後臺文章管理列表顯示特色圖像(Featured Image)

特色圖像(featured image)是WordPress 3.0+ 的一個不錯的功能,允許你給文章設置一個圖片作為它的縮略圖顯示。瞭解下什麼是特色圖像。為瞭直觀地瞭解每篇文章的特色圖像是什麼,建議在WordPress後臺文章管理列表顯示每篇文章的特色圖像,如下圖所示:

featured-image-column-wpdaxue_com

實現的方法很簡單,使用 Featured Image Column 外掛即可。在後臺外掛安裝界面搜索 Featured Image Column 即可線上安裝,或者下載 Featured Image Column

啟用該外掛後,你就會發現在後臺文章管理列表多出瞭一欄顯示特色圖像。

如果你希望沒有特色圖像時在後臺文章列表顯示默認的圖像,可以在主題的 functions.php 中添加下面的代碼:

1
2
3
4
5
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' );

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 中添加下面的代碼:

1
2
3
4
5
6
/**
 * @use '.featured-image.column-featured-image img {}'
 */
function my_custom_featured_image_css() {
    return trailingslashit( get_stylesheet_directory_uri() ) . 'css/featured-image.css'; //css文件的URL
add_filter( 'featured_image_column_css', 'my_custom_featured_image_css' );

/**
* @use ‘.featured-image.column-featured-image img {}’
*/
function my_custom_featured_image_css() {
return trailingslashit( get_stylesheet_directory_uri() ) . ‘css/featured-image.css’; //css文件的URL
add_filter( ‘featured_image_column_css’, ‘my_custom_featured_image_css’ );

然後將名為 featured-image.css 的CSS文件上傳到當前主題下的 css 文件夾。

以下是外掛默認添加的css代碼,你可以將其添加到 featured-image.css 並進行修改:

1
2
3
4
5
6
7
8
9
10
11
th#featured-image,
td.featured-image.column-featured-image {
	max-height: 50px;
	max-width: 50px;
	width: 50px;
}
.featured-image.column-featured-image img {
	max-height: 32px;
	max-width: 36px;
	-ms-interpolation-mode: bicubic;
}

th#featured-image,
td.featured-image.column-featured-image {
max-height: 50px;
max-width: 50px;
width: 50px;
}
.featured-image.column-featured-image img {
max-height: 32px;
max-width: 36px;
-ms-interpolation-mode: bicubic;
}

發佈留言