所以我使用了以下插件WP Term Images来为每个类别设置图像并且它有效......但仅当该类别至少有一个帖子时才有效。
<?php // image id is stored as term meta
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
$image_id = get_term_meta( $category_id, 'image', true );
// image data stored in array, second argument is which image size to retrieve
$image_data = wp_get_attachment_image_src( $image_id, 'large' );
// image url is the first item in the array (aka 0)
$image = $image_data[0];
if ( ! empty( $image ) ) {echo $image;}; ?>
但是,当某个类别中没有帖子时,即使该类别包含图片附件,也会返回以下错误
Notice: Undefined offset: 0 in [..]/theme/archive.php on line 14
Notice: Trying to get property of non-object in [..]/theme/archive.php on line 14