我可以自定义分类法,但要有与之相关联的图标吗?
示例:我想要一份我用于公文包项目的技能列表(例如PHP、MySQL、CSS等)
我想显示图标,而不是文本。
所以我需要为我的分类法设置图标。
我该怎么做<这很复杂吗?它看起来怎么样?或者它是什么样子<步骤?
我可以自定义分类法,但要有与之相关联的图标吗?
示例:我想要一份我用于公文包项目的技能列表(例如PHP、MySQL、CSS等)
我想显示图标,而不是文本。
所以我需要为我的分类法设置图标。
我该怎么做<这很复杂吗?它看起来怎么样?或者它是什么样子<步骤?
我建议Taxonomy Images Plugin. 它说它是Beta版的,但我已经在一些网站上使用过,效果很好。
我只想将它构建到你的主题中,并将图标存储在你的主题文件夹中。
当您在前端显示它们时,只需检查图标图像,而不是显示术语:
foreach( $terms as $term ) {
if( file_exists( TEMPLATEPATH . \'images/\' . $term->slug . \'.png\' )
//show image
}
如果确实需要通过WP Admin上传,则可以连接到编辑术语页面并添加另一个表单字段:add_action( \'my_taxonomy_edit_form_fields\', \'my_callback_function_to_show_upload\' );
如果确实使用了上述方法,则还必须更改表单的“enc类型”。你不是唯一一个问这个问题的人,所以我贴了一个how to add extra fields to custom taxonomies 只有当您想自己添加它们时,才可以这样做。如果没有,那么JoeHoyle将图片上传到主题目录并根据->ID.png这个词命名的解决方案将获得我的+1票。
根据@joehoyle的回答,你可以这样做。嗯,这必须适应你的主题模板。
<?php
$terms = get_the_terms( $post->ID, \'custom_cat\' );
$numcat=sizeof( $terms );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, \'custom_cat\' );
if( file_exists( TEMPLATEPATH .\'/images/\'. $term->slug .\'.png\' ) ) { ?>
<a rel="tag" href="<?php echo $term_link; ?>">
<img
title="<?php echo $term->name ;?>"
style="height: 21px; width: 21px;"
src="<?php
echo get_template_directory_uri() .
\'/images/\' .
$term->slug .
\'.png\'
?>"
alt="<?php echo $term->name; ?>"
>
</a>
|
<?php } else { ?>
<a rel="tag" href="<?php echo $term_link; ?>">
<?php echo $term->name; ?>
</a>
|
<?php }
}
?>
这个Advanced Custom Fields plugin 处理得很好;您可以通过ACF将“image”自定义字段添加到自定义分类法中,它将正确显示在分类法的控制面板页面上,然后您可以使用ACF的get_field()
函数将其显示在模板中。