WordPress: How to show product term’s description in Front-End?
-
Hello
In WooCommerce plugin of WordPress, as you know there is one taxonomy called “Attributes” that let us add some filters (attributes) for products and assign them to products. Each attributes can have multiple items like a category with multiple sub-categories that called “Attribute terms”. When we want add or edit attribute terms, we have a “Description” textarea.
I have WoodMart theme so I would like to display these descriptions in Front-End of their pages.
the URL of the attribute’s page in front-end is like this:
example.com/product-category/sofas/?filter_brand=4mariani
And this is code I’m using but doesn’t add nothing to these pages:
function display_attribute_term_description() { // Get the current attribute term $term = get_queried_object(); // Check if the term exists and has a description if ($term && !empty($term->description)) { echo '<div class="attribute-description">' . wpautop($term->description) . '</div>'; } } add_action('woocommerce_before_main_content', 'display_attribute_term_description');
I’m also trying to make a category page with Elementor Plugin, but there isn’t any dynamic widget for the term’s description. so I’ve added a text widget and tried to inject the term’s description into it and still doesn’t work:
function inject_term_description_into_archive_description( $content ) { if ( is_category() ) { $term_description = term_description(); $content = $term_description . $content; } return $content; } add_filter( 'get_the_archive_description', 'inject_term_description_into_archive_description' );
After this way, I’ve tried to add a custom widget in Elementor for showing term’s description and still doesn’t work.
<?php use Elementor\Widget_Base; class Custom_Term_Description_Widget extends Widget_Base { public function get_name() { return 'custom-term-description-widget'; } public function get_title() { return __( 'Custom Term Description', 'your-text-domain' ); } public function get_icon() { return 'eicon-text-area'; } public function get_categories() { return [ 'general' ]; } protected function _register_controls() { $this->start_controls_section( 'section_content', [ 'label' => __( 'Content', 'your-text-domain' ), ] ); $this->add_control( 'term_description', [ 'label' => __( 'Term Description', 'your-text-domain' ), 'type' => \Elementor\Controls_Manager::WYSIWYG, 'dynamic' => [ 'active' => true, 'categories' => [ \Elementor\Modules\DynamicTags\Module::POST_META_CATEGORY, ], 'default' => \Elementor\Modules\DynamicTags\ACF\Module::TEXT_CATEGORY, ], ] ); $this->end_controls_section(); } protected function render() { $settings = $this->get_settings_for_display(); echo wpautop( $settings['term_description'] ); } }
and then i just added this to my?
functions.php
?child-theme file.function register_custom_term_description_widget() { require_once get_template_directory() . '/custom-term-description-widget.php'; \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new Custom_Term_Description_Widget() ); } add_action( 'elementor/widgets/widgets_registered', 'register_custom_term_description_widget' );
Any idea what should I do?
The page I need help with: [log in to see the link]
- The topic ‘WordPress: How to show product term’s description in Front-End?’ is closed to new replies.