Ecommerce Shopify WordPress Discussion

WordPress: Problem with entering a shortcode in an elementor

I have the following php function: add_shortcode('mysubcategory', 'display_parent_subcategory'); function display_parent_subcategory($atts) { $atts = shortcode_atts(array( 'category_slug' => '', ), $atts, 'show_parent'); $category_slug = sanitize_text_field($atts['category_slug']); // Get the specified parent category $parent_category = get_term_by('slug', $category_slug, 'product_cat'); if (isset($parent_category) && !is_wp_error($parent_category)) { echo '<ul class="menu_subcategory">'; display_child_categories_recursive($parent_category->term_id); echo '</ul>'; } else { echo 'Invalid category slug'; } } function display_child_categories_recursive($parent_category_id) { // Get all child categories of the current category, including empty ones $child_categories = get_terms(array( 'taxonomy' => 'product_cat', 'parent' => $parent_category_id, 'hide_empty' => false, )); foreach ($child_categories as $child_category) { echo '<li class="cat_id_' . $child_category->term_id . '"> <a href="' . get_term_link($child_category->term_id) . '">' . $child_category->name . '</a>'; // Recursively call the function for each child category echo '<ul>'; display_child_categories_recursive($child_category->term_id); echo '</ul>'; echo '</li>'; } } The problem is that after typing in Elementor, using the shortcode widget, the result in Elementor is displayed except where it is placed and in the upper left of the editor.This only occurs in the Elementor editor itself. The page renders correctly but it is annoying. view in Elementor Maybe it comes from some problem in the php code.
For shortcodes, if you are going to echo or output inside the function, you need to use ob_start and ob_get_clean to solve this problem, like this: add_shortcode('mysubcategory', 'display_parent_subcategory'); function display_parent_subcategory($atts) { $atts = shortcode_atts(array( 'category_slug' => '', ), $atts, 'show_parent'); $category_slug = sanitize_text_field($atts['category_slug']); // Get the specified parent category $parent_category = get_term_by('slug', $category_slug, 'product_cat'); ob_start(); if (isset($parent_category) && !is_wp_error($parent_category)) { echo '<ul class="menu_subcategory">'; display_child_categories_recursive($parent_category->term_id); echo '</ul>'; } else { echo 'Invalid category slug'; } return ob_get_clean(); } function display_child_categories_recursive($parent_category_id) { // Get all child categories of the current category, including empty ones $child_categories = get_terms(array( 'taxonomy' => 'product_cat', 'parent' => $parent_category_id, 'hide_empty' => false, )); ob_start(); foreach ($child_categories as $child_category) { echo '<li class="cat_id_' . $child_category->term_id . '"> <a href="' . get_term_link($child_category->term_id) . '">' . $child_category->name . '</a>'; // Recursively call the function for each child category echo '<ul>'; display_child_categories_recursive($child_category->term_id); echo '</ul>'; echo '</li>'; } return ob_get_clean(); } Reference: https://codex.wordpress.org/Shortcode_API#Output

January 20, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.