This guide is a demonstration of the ACF (Advanced Custom Fields) How to add custom fields to a categorized term and then modify the template file HTML for the term.
Terms can be items such as WordPress categories, tags, or other custom taxonomies that have been added to the site. For example.WooCommerce Popular plugins such as this add custom taxonomies when activated.
![图片[1]-使用ACF向WordPress分类术语添加自定义字段的完整指南](https://www.361sale.com/wp-content/uploads/2024/11/20241118142906981-image.png)
Adding Fields
The Advanced Custom Fields plugin makes it easy to add custom fields to category terms, follow the steps below.
- exist"Custom fields"In the Administration screen, click"Add new"button to create a new field group
- Add fields you want to see when editing a taxonomy term
- in positionlower case, selectClassification terminologyrule and select the appropriate value to display this field group
![图片[2]-使用ACF向WordPress分类术语添加自定义字段的完整指南](https://www.361sale.com/wp-content/uploads/2024/11/20241118141834744-image.png)
edit a field
Once you have created a field group and assigned it to the Category Term Edit screen, you can edit the field values by navigating to the appropriate taxonomy. For example, if the field group has been assigned to a post category, navigate to thePosts > CategoryThe
![图片[3]-使用ACF向WordPress分类术语添加自定义字段的完整指南](https://www.361sale.com/wp-content/uploads/2024/11/20241118141913840-image.png)
Display Fields
category.phpJust edit in the theme,tag.phpor file, you can easily customize the HTML for WordPress taxonomy terms! taxonomy.php. Depending on the topic, HTML can also be customized using template sections or filters.
This example shows how to modify thecategory.phpTemplates for twentyseventeen theme and export category images and colors to style tags.
<?php
/**
* The template for displaying category archive pages
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package WordPress
* @subpackage Twenty_Seventeen
* @since 1.0
* @version 1.0
*/
get_header();
// get the current taxonomy term
$term = get_queried_object();
// vars
$image = get_field('image', $term);
$color = get_field('color', $term);
?>
<style type="text/css">
.entry-title a {
color: <?php echo $color; ?>;
}
<?php if( $image ): ?>
.site-header {
background-image: url(<?php echo $image['url']; ?>);
}
<?php endif; ?>
</style>
<div class="wrap">
<?php // Remaining template removed from example ?>
(for) instance
Here's how the above code looks in the browser.
Attention:Site title images now reflect category images and all entry titles are green.
![图片[4]-使用ACF向WordPress分类术语添加自定义字段的完整指南](https://www.361sale.com/wp-content/uploads/2024/11/20241118142039564-image.png)
Link to this article:https://www.361sale.com/en/27095/The article is copyrighted and must be reproduced with attribution.























![表情[wozuimei]-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/themes/zibll/img/smilies/wozuimei.gif)
![表情[baoquan]-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/themes/zibll/img/smilies/baoquan.gif)

No comments