@ligisayan

Как получить и вывести миниатюры и описание категорий для пунктов меню?

Всем привет! Магазин на wordpress+wocommerce. Есть меню категорий товаров, объект меню выводится в таком виде
[PHP]object(WP_Post)#9911 (40) { ["ID"]=> int(8162) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2015-12-28 13:18:05" ["post_date_gmt"]=> string(19) "2015-12-28 10:18:05" ["post_content"]=> string(1) " " ["post_title"]=> string(0) "" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(4) "8162" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2015-12-29 12:05:10" ["post_modified_gmt"]=> string(19) "2015-12-29 09:05:10" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(33) "http://boats.likemedia.by/?p=8162" ["menu_order"]=> int(1) ["post_type"]=> string(13) "nav_menu_item" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" ["db_id"]=> int(8162) ["menu_item_parent"]=> string(1) "0" ["object_id"]=> string(3) "257" ["object"]=> string(11) "product_cat" ["type"]=> string(8) "taxonomy" ["type_label"]=> string(31) "Категория товара" ["url"]=> string(52) "http://домен/product-category/on-water/" ["title"]=> string(13) "На воде" ["target"]=> string(0) "" ["attr_title"]=> string(0) "" ["description"]=> string(0) "" ["classes"]=> array(5) { [0]=> string(0) "" [1]=> string(9) "menu-item" [2]=> string(23) "menu-item-type-taxonomy" [3]=> string(28) "menu-item-object-product_cat" [4]=> string(22) "menu-item-has-children" } ["xfn"]=> string(0) "" ["current"]=> bool(false) ["current_item_ancestor"]=> bool(false) ["current_item_parent"]=> bool(false) } [/PHP]

Вопрос: как получить и вывести миниатюры и описание категорий для пунктов меню? (во вложенных пунктах меню есть одинаковые названия категорий)

Вот таким способом можно вывести миниатюры категорий, но как их вывести для объектов меню?
$taxonomy     = 'product_cat';
                        $empty        = 0;
                        $args = array(
                            'taxonomy'     => $taxonomy,
                            'hide_empty'   => $empty
                        );
                        $all_categories = get_categories( $args );
                        foreach ($all_categories as $cat) {
                            $category_id = $cat->term_id;
                            $thumbnail_id = get_woocommerce_term_meta( $category_id, 'thumbnail_id', true );
                            $cat_image = wp_get_attachment_url( $thumbnail_id );       
                            echo '<img src="'.$cat_image.'" alt="" />';     
                        }
  • Вопрос задан
  • 254 просмотра
Пригласить эксперта
Ответы на вопрос 1
@ligisayan Автор вопроса
У меня получилось сделать, но совсем как-то не изящно все выглядит. Хотелось бы для надежности по якорю сравнение искать или встроенной функцией какой-либо обойти, чтобы исключить цикл
$itemcat = $item->title;
                    $taxonomy     = 'product_cat';
    								$empty        = 0;
    								$args = array(
    									'taxonomy'     => $taxonomy,
    									'hide_empty'   => $empty
    								);
    								$all_categories = get_categories( $args );
    								foreach ($all_categories as $cat) {
                                        $category_name = $cat->name;
                                        if($category_name==$itemcat) {
                                            $cat_name = $category_name;
                                            $category_id = $cat->term_id; 
    								        $thumbnail_id = get_woocommerce_term_meta( $category_id, 'thumbnail_id', true );
    								        $cat_image = wp_get_attachment_url( $thumbnail_id );
                                            $description = $cat->description;
                                        }
                                    }
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы