admin管理员组文章数量:1129441
I've been trying without luck to get a custom taxonomy slug to appear as a class in the body tag of WordPress. Perhaps it has to do with the fact that I'm using a custom taxonomy rather than the build in one.
I'm registering a custom PAGE taxonomy in functions.php as follows and it works fine.
add_action( 'init', 'create_page_taxonomies' );
function create_page_taxonomies() {
register_taxonomy('page_section', 'page', array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Page Section', 'taxonomy general name' ),
'singular_name' => _x( 'page-section', 'taxonomy singular name' ),
'search_items' => __( 'Search Sections' ),
'all_items' => __( 'All Sections' ),
'parent_item' => __( 'Parent Section' ),
'parent_item_colon' => __( 'Parent Section:' ),
'edit_item' => __( 'Edit Section' ),
'update_item' => __( 'Update Section' ),
'add_new_item' => __( 'Add New Section' ),
'new_item_name' => __( 'New Section Name' ),
'menu_name' => __( 'Sections' )
),
'public' => true,
'rewrite' => array(
'slug' => 'page-section',
'with_front' => false,
'hierarchical' => true
)
));
}
This creates a 'Sections' menu option under Pages. It works just like Post Categories. I then created a taxonomy Term labelled Trip Planner (slug: trip-planner) I then add the following to functions.php to try and get the taxonomy term slug name to appear as a class on the body tag.
add_filter( 'body_class', 'themeprefix_add_taxonomy_class' );
function themeprefix_add_taxonomy_class( $classes ){
if( is_singular() ) {
global $post;
$taxonomy_terms = get_the_terms($post->ID, 'page-section');
if ( $taxonomy_terms ) {
foreach ( $taxonomy_terms as $taxonomy_term ) {
$classes[] = 'section-' . $taxonomy_term[0]->slug;
}
}
}
return $classes;
}
This second snippet is 'sort of' working, when added and I view page source, I can see 'section-' appear as a class on the body tag but the term slug value which is supposed to follow the dash is not present. My body tag now looks like this.
<body class="page-template page-template-elementor_header_footer page page-id-451 wp-custom-logo section- elementor-default elementor-template-full-width elementor-kit-6 elementor-page elementor-page-451">
Mostly Elementor page builder classes but the 'section-' is present. I edited the second function to reference the 'page-section' slug defined in the register_taxonomy function.
Additional info if helpful.
- WordPress: 6.4.2
- Page Builder: Elementor
- Custom Page Taxonomy Slug: page-section
- Taxonomy Term Name Example: Trip Planner
- Taxonomy Term Slug Example: trip-planner
- Expected Body Class to be Added: section-trip-planner
I must be missing something?!?
UPDATE:
The PHP error generated (see comment below) when I corrected the page-section to page_section let me to the solution. $taxonomy_term[0] is not an array it is an Std Object, therefor I just removed '[0]' and now everything works fine.
I've been trying without luck to get a custom taxonomy slug to appear as a class in the body tag of WordPress. Perhaps it has to do with the fact that I'm using a custom taxonomy rather than the build in one.
I'm registering a custom PAGE taxonomy in functions.php as follows and it works fine.
add_action( 'init', 'create_page_taxonomies' );
function create_page_taxonomies() {
register_taxonomy('page_section', 'page', array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Page Section', 'taxonomy general name' ),
'singular_name' => _x( 'page-section', 'taxonomy singular name' ),
'search_items' => __( 'Search Sections' ),
'all_items' => __( 'All Sections' ),
'parent_item' => __( 'Parent Section' ),
'parent_item_colon' => __( 'Parent Section:' ),
'edit_item' => __( 'Edit Section' ),
'update_item' => __( 'Update Section' ),
'add_new_item' => __( 'Add New Section' ),
'new_item_name' => __( 'New Section Name' ),
'menu_name' => __( 'Sections' )
),
'public' => true,
'rewrite' => array(
'slug' => 'page-section',
'with_front' => false,
'hierarchical' => true
)
));
}
This creates a 'Sections' menu option under Pages. It works just like Post Categories. I then created a taxonomy Term labelled Trip Planner (slug: trip-planner) I then add the following to functions.php to try and get the taxonomy term slug name to appear as a class on the body tag.
add_filter( 'body_class', 'themeprefix_add_taxonomy_class' );
function themeprefix_add_taxonomy_class( $classes ){
if( is_singular() ) {
global $post;
$taxonomy_terms = get_the_terms($post->ID, 'page-section');
if ( $taxonomy_terms ) {
foreach ( $taxonomy_terms as $taxonomy_term ) {
$classes[] = 'section-' . $taxonomy_term[0]->slug;
}
}
}
return $classes;
}
This second snippet is 'sort of' working, when added and I view page source, I can see 'section-' appear as a class on the body tag but the term slug value which is supposed to follow the dash is not present. My body tag now looks like this.
<body class="page-template page-template-elementor_header_footer page page-id-451 wp-custom-logo section- elementor-default elementor-template-full-width elementor-kit-6 elementor-page elementor-page-451">
Mostly Elementor page builder classes but the 'section-' is present. I edited the second function to reference the 'page-section' slug defined in the register_taxonomy function.
Additional info if helpful.
- WordPress: 6.4.2
- Page Builder: Elementor
- Custom Page Taxonomy Slug: page-section
- Taxonomy Term Name Example: Trip Planner
- Taxonomy Term Slug Example: trip-planner
- Expected Body Class to be Added: section-trip-planner
I must be missing something?!?
UPDATE:
The PHP error generated (see comment below) when I corrected the page-section to page_section let me to the solution. $taxonomy_term[0] is not an array it is an Std Object, therefor I just removed '[0]' and now everything works fine.
Share Improve this question edited Dec 20, 2023 at 0:03 Overspeed asked Dec 19, 2023 at 4:34 OverspeedOverspeed 112 bronze badges 2- While looking over my code again, I may have found an issue. My custom Taxonomy is assigned the ID of 'page_section' and the slug is assigned the name of page-section. My code to add the body class is calling the taxonomy slug instead of it's ID. I modified my body class code to use page_section instead of page-section. That didn't work, instead of printing the body class the site just errors out with the message "There has been a critical error on this website.". – Overspeed Commented Dec 19, 2023 at 23:02
- When I change page-section to page_section and the site goes down with critical error, the following is shown in the PHP error log: [19-Dec-2023 23:20:23 UTC] PHP Fatal error: Uncaught Error: Cannot use object of type WP_Term as array in /home/j8k4tkm/public_html/wp-content/themes/hello-theme-child-master/functions.php:181 – Overspeed Commented Dec 19, 2023 at 23:22
2 Answers
Reset to default 0To add the custom taxonomy slug to the body classes of your WordPress site, you can use the body_class filter along with your custom code. Below is your example of how you can achieve this:
function themeprefix_add_taxonomy_class($classes) {
global $post;
if (is_page()) {
$taxonomy = 'page_section';
$terms = get_the_terms($post->ID, $taxonomy);
if ($terms && !is_wp_error($terms)) {
foreach ($terms as $term) {
$classes[] = 'section-' . $term->slug;
}
}
}
return $classes;
}
add_filter('body_class', 'themeprefix_add_taxonomy_class');
$taxonomy_terms
is already an array, so you either do the foreach
loop to add all the associated terms as seperate classes (in that case you don't need the first index, i.e 0) or add only the first term (in that case you don't need the loop).
To add all the terms..
add_filter( 'body_class', 'themeprefix_add_taxonomy_class' );
function themeprefix_add_taxonomy_class( $classes ){
if( is_singular() ) {
global $post;
$taxonomy_terms = get_the_terms($post->ID, 'page-section');
if ( $taxonomy_terms && !empty($taxonomy_terms) ) {
foreach ( $taxonomy_terms as $taxonomy_term ) {
$classes[] = 'section-' . $taxonomy_term->slug;
}
}
}
return $classes;
}
Or, just the first term..
add_filter( 'body_class', 'themeprefix_add_taxonomy_class' );
function themeprefix_add_taxonomy_class( $classes ){
if( is_singular() ) {
global $post;
$taxonomy_terms = get_the_terms($post->ID, 'page-section');
if ( $taxonomy_terms && isset($taxonomy_term[0]) ) {
$classes[] = 'section-' . $taxonomy_term[0]->slug;
}
}
return $classes;
}
本文标签: customizationHow to get a custom taxonomy slug in body classes
版权声明:本文标题:customization - How to get a custom taxonomy slug in body classes 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736713474a1949070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论