如果我们能够在类别或标签旁边显示图像,则有时WordPress网站会看起来更好。 例如,我们可以在“ CSS”类别中添加有关CSS的图像,或在“ HTML”类别中添加HTML5徽标(如下所示)。

早在2.9版中,WordPress就添加了为帖子, 页面以及自定义帖子类型 添加图像缩略图 (后重命名为Featured Image )的功能。 该图像缩略图功能不包括类别,标签和自定义分类法 ,甚至在更高版本中也不例外(可能自定义分类法(可能在WordPress 4.4中除外))。

感谢这个WordPress技巧和分类分类缩略图插件,我们可以对我们的帖子类别和标签执行此操作。 让我们看看如何用几行代码来完成。

入门

首先,在您的WordPress网站中部署此插件。 您可以通过插件>添加新插件或通过FTP安装插件。 激活插件后,转到发布>类别 。 现在,您应该可以找到“ 设置缩略图 ”按钮。


<div> 
    <img src="https://s2.51cto.com/images/blog/202404/08193901_6613d75586e662731.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="">
</div>

wordpress tags wordpress tag缩略图_wordpress tags


单击此按钮将显示WordPress Media Manager 。 在这里,您可以选择之前上传的图像作为类别图像,也可以上传图像,对其进行自定义,最后选择它作为类别(或标签)的图像缩略图。

该图像将显示在“ 类别”表中,使您可以查看附加了图像类别的类别。


<div> 
    <img src="https://s2.51cto.com/images/blog/202404/08193901_6613d755e29c763167.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="">
</div>


wordpress tags wordpress tag缩略图_wordpress tags_02


模板标签

该插件带有一些方便的模板标签,用于检索类似于“发布缩略图”模板标签的图像。 这样,我们可以轻松检索该词的缩略图。

  • get_term_thumbnail_id($ term_taxonomy_id) :获取分类术语缩图ID。
  • has_term_thumbnail($ term_taxonomy_id) :检查分类术语是否具有缩略图。
  • get_term_thumbnail($ term_taxonomy_id,$ size ='post-thumbnail',$ attr =”) :检索分类术语缩览图。

如上所示,这些功能需要分类法ID(类别,标签或自定义分类法ID),您可以使用term_taxonomy_id函数进行检索。 该插件还提供了一些其他功能,例如设置和删除缩略图,但这些功能目前已经足够。

如何显示缩略图

获取条款列表

首先,我们使用get_terms()函数获取指定分类法的术语列表–在这种情况下,我们将从帖子类别中获取术语。

<?php 
    $taxonomy = 'category';
    $args = array(
        'orderby'           => 'name', 
        'order'             => 'ASC',
        'hide_empty'        => true, 
        'exclude'           => array(), 
        'exclude_tree'      => array(), 
        'include'           => array(),
        'number'            => '', 
        'fields'            => 'all', 
        'slug'              => '',
        'parent'            => '',
        'hierarchical'      => true, 
        'child_of'          => 0,
        'childless'         => false,
        'get'               => '', 
        'name__like'        => '',
        'description__like' => '',
        'pad_counts'        => false, 
        'offset'            => '', 
        'search'            => '', 
        'cache_domain'      => 'core',
    );
    $terms = get_terms($taxonomy, $args);
?>

输出是一个包含每个术语信息的数组,包括term_idnameslugterm_groupterm_taxonomy_iddescription等。现在,我们需要使用foreach循环在列表中显示术语的name ,如下所示。

<?php 
	if (!empty($terms) && !is_wp_error($terms) ){
		echo '<p>'. $taxonomy .':</p>';
		echo '<ul>';
		foreach ($terms as $term) {
			echo $term->name;
		}
		echo '</ul>';
    }
?>

结果看起来像这样:

<div>

<img src="https://s2.51cto.com/images/blog/202404/08193902_6613d7564371313647.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="">
</div>


wordpress tags wordpress tag缩略图_列表_03


在这里,我们有CSS,HTML,JavaScript,jQuery和PHP。 我们为每个术语附加了相应的图像(徽标或图标)。 现在,我们需要知道如何显示它们。

显示缩略图

为了显示图像缩略图,我们将像前面的代码一样扩展foreach循环。

我们添加模板标签get_term_thumbnail()以获得缩略图,并且还添加指向该术语的存档页面的链接。

if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    echo '<ul>';
        foreach ( $terms as $term ) {
            echo '<li><a href="/index.php/' . $taxonomy . '/' . $term->slug . '">' . $term->name . get_term_thumbnail( $term->term_taxonomy_id, $size = 'category-thumb', $attr = '' ) . '</a></li>';
    }
    echo '</ul>';
}

结果(添加CSS之后)如下所示:

<div>     <img src="https://s2.51cto.com/images/blog/202404/08193902_6613d7567279136539.gif" alt="">
</div>

wordpress tags wordpress tag缩略图_php_04


该插件具有选择检索所有术语或仅检索带有缩略图的术语的选项。 为此,请在get_terms()函数上使用以下参数:

$taxonomy = 'category';
$args = array(
    'with_thumbnail' => true, // true = retrieve terms that has thumbnail, false = retrieve all terms
);
$terms = get_terms($taxonomy, $args);
适用于其他分类法

如前所述,您不仅可以将此插件应用于类别,还可以应用于其他分类法,例如标签链接类别自定义分类法 。 该插件可用于在任何分类法上启用“图像缩略图”,就像在“邮寄和页面”中一样。