How to display woocommerce products by category

o ‘slug . ‘ \
Echo $term – > name;
Echo \
Echo \
Echo \
}
Echo \
}
}
add_action( ‘woocommerce_before_shop_loop’, ‘woocommerce_product_category’, 100 ); The results are as follows: However, in order to better display the product, several custom CSS are required. Do this later in this guide.
How code works this code uses the woocomer_product_category() function of the output category or subcategory to work before executing the loop of the output product. Because you can plug-in functionality, you can redefine the theme. c) It is very easy to get the subcategories of the woocommerce product category by using the custom function of listing the subcategories of the woocommerce product category and tracking the parent product category. Log in to the woodpress site, list the subcategories of the woocommerce product category, and access the dashboard as an administrator. In the panel menu, click shape Menu > Theme Editor. After the theme editor page opens, browse the theme function file to add the function of listing the subcategories of woocommerce product category. Add the following code to the PHP file: Function woocommerce_get_product_category_of_subcares ($category_slug){
$terms_html = array ();
$taxonomy = \
$parent=get_term_by(“slug”、$category_slug、$taxonomy);
$children_ids = get_term_children( $parent->term_id, $taxonomy );
foreach($children_ids as $children_id){
$term = get_term( $children_id, $taxonomy );
$term_link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $term_link ) ) $term_link = ”;
$terms_html[] = ‘slug . ‘ \
}
Return \
}The results are as follows: How the code works. The wp_term object gets the parent of the product category. Then get the sub ID of the array, and finally execute the loop through the sub ID array, and the sub categories are displayed in HTML.
d) Before woocommerce creates a code recognition plug-in for outputting archive categories and products, the first step is to determine how woocommerce outputs categories and subcategories. That is, to find relevant functions, you must manually find the woocommerce source code. To simplify the process, save archive product in the template folder. Just find PHP. Woocommerce is used to display the file of the archive page. Now we need to find the output category and product code.

As can be seen from the above image, the categories are not well sorted. That is, you must add custom styles. But before that, let’s take a look at how the code works. How the code works, the function we created identifies the object of the current query and defines its ID as $parentid. Then use get_terms() to identify the term that the item currently queried is the parent. If this is the default shopping page, the top-level category is returned; if it is a category archive, the subcategory is returned. In addition, the function checks for terms before opening each ring and UL element for. It creates a Li element for each term, then outputs category image using woocomer_subcategory_thumbnail(), and then enters the category name in the link to the archive file.

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *