Last active
February 20, 2021 15:40
-
-
Save Prroffessorr/fbbd85c82310719939af8c4ad46bcb2c to your computer and use it in GitHub Desktop.
WooCommerce ( Product Handling Cheat Sheet ) ( archive-product.php )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //Получение категорий | |
$prod_cat_args = array( | |
'taxonomy' => 'product_cat', | |
'orderby' => 'id', // здесь по какому полю сортировать | |
'hide_empty' => false, // скрывать категории без товаров или нет | |
'parent' => 0 // id родительской категории | |
); | |
?> | |
<ul> | |
<?php //Получение всего списка категорий | |
$categories = get_categories( $prod_cat_args ); | |
foreach ( $categories as $category ) : | |
//Получение списка дочерних категорий ( Получены по таксономии ) | |
$subcategories = get_terms( $category->taxonomy , array( | |
'parent' => $category->term_id, | |
'hide_empty' => false | |
)); ?> | |
<li> | |
<span> <?php echo $category->name; ?> </span> | |
<?php | |
//Проверка что дочерняя категория существует | |
if ($subcategories): ?> | |
<ul> | |
<?php //Вывод дочерних категорий | |
foreach ( $subcategories as $subcategory ): | |
$term = get_term_by( 'term_id', $subcategory->term_id, 'product_cat'); ?> | |
<li> | |
<a href="<?php echo get_category_link($term); ?>"><?php echo $term->name; ?></a> | |
</li> | |
<?php | |
endforeach; ?> | |
</ul> | |
<?php | |
endif; ?> | |
</li> | |
<?php | |
endforeach; ?> | |
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Получение текущей категории (Для страницы категории) | |
$category = get_queried_object(); | |
//Получение списка дочерних категорий | |
$subcategories = get_terms( $category->taxonomy , array( | |
'parent' => $category->term_id, | |
'hide_empty' => false | |
) ); | |
//Проверка на то что дочерний элемент существует и что мы находимся на странице категории | |
if ($subcategories && is_product_category()): ?> | |
<ul> | |
<?php //Получение и вывод дочерних категорий | |
foreach ( $subcategories as $subcategory ): ?> | |
<li> | |
<a href="<?php echo get_category_link($subcategory->term_id); ?>" ><?php echo $subcategory->name; ?> </a> | |
</li> | |
<?php | |
endforeach; ?> | |
</ul> | |
<?php | |
endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment