admin管理员组文章数量:1327996
I'm building a Wordpress theme and I am currently finishing the category.php
page. For this page, I would like the user to select two colors (background color and title/text color) and use them in the category.php
page.
I searched on the internet for examples on how to do this, but I only found some pieces of code without explanation (example). I also encountered something about functions for custom fields being deprecated in Wordpress.
Can anyone show me how I add two custom fields for color to each category and how I should apply that in the theme? Or point me to a guide on custom fields in Wordpress?
I don't want to use inline CSS, so I think that we should use wp_add_inline_styles
to apply the styling. So say that color 1 should be applied to .background
and color 2 to .title
for each different category, what would I put in my wp_add_inline_styles
?
Many thanks!
I'm building a Wordpress theme and I am currently finishing the category.php
page. For this page, I would like the user to select two colors (background color and title/text color) and use them in the category.php
page.
I searched on the internet for examples on how to do this, but I only found some pieces of code without explanation (example). I also encountered something about functions for custom fields being deprecated in Wordpress.
Can anyone show me how I add two custom fields for color to each category and how I should apply that in the theme? Or point me to a guide on custom fields in Wordpress?
I don't want to use inline CSS, so I think that we should use wp_add_inline_styles
to apply the styling. So say that color 1 should be applied to .background
and color 2 to .title
for each different category, what would I put in my wp_add_inline_styles
?
Many thanks!
Share Improve this question asked Jul 21, 2020 at 9:18 ralphjsmitralphjsmit 4026 silver badges23 bronze badges1 Answer
Reset to default 2To add custom fields and save/update meta data to terms, you basically need to use these action hooks.
custom fields html for new term
{$taxonomy}_add_form_fields
save custom field data for new term, get data from $_POST
created_{$taxonomy}
custom fields html for existing term
{$taxonomy}_edit_form_fields
save custom field data for existing term, get data from $_POST
edited_{$taxonomy}
Save / update term meta
add_term_meta()
update_term_meta()
Get term meta
get_term_meta()
You can see the general idea how to use these hooks here, https://wordpress.stackexchange/a/296767/144392
Use saved meta data
One way to use the save term meta data is to get it directly on the category template and add it as inline style to an element.
// category.php
$color = sanitize_hex_color(get_term_meta(get_queried_object_id(), 'color', true));
<h1<?php echo $color ? ' style="color: ' . $color . ';"' : ''; ?>>Category title</h1>
Another way is to add the meta data as inline style with wp_add_inline_style()
on wp_enqueue_scripts
action.
// functions.php
function wpdocs_styles_method() {
if ( is_category() ) {
$id = get_queried_object_id();
$title = sanitize_hex_color(get_term_meta($id, 'title_color', true));
$bg = sanitize_hex_color(get_term_meta($id, 'bg_color', true));
$custom_css = '';
if ( $title ) {
$custom_css .= ".title {color: {$color};}";
}
if ( $bg ) {
$custom_css .= " .background {background-color: {$bg};}";
}
if ( $custom_css ) {
// update the slug
wp_add_inline_style( 'your-theme-styles-slug', trim($custom_css) );
}
}
}
add_action( 'wp_enqueue_scripts', 'wpdocs_styles_method' );
Color picker
You can use wpColorPicker
script to turn text input fields into nice color pickers. Usage examples https://make.wordpress/core/2012/11/30/new-color-picker-in-wp-3-5/ and Color picker for posts and pages
本文标签: categoriesHow to add custom color fields to the category edit page
版权声明:本文标题:categories - How to add custom color fields to the category edit page? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742235836a2438063.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论