L
L
ligisayan2015-12-30 10:21:51
WordPress
ligisayan, 2015-12-30 10:21:51

How to get and display thumbnails and category descriptions for menu items?

Hello! Shop on wordpress + wocommerce. There is a menu of product categories, the menu object is displayed in this form

[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]

Question: how to get and display thumbnails and category descriptions for menu items? (nested menu items have the same category names)
This is how you can display category thumbnails, but how do you display them for menu objects?
$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="" />';     
                        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
ligisayan, 2015-12-30
@ligisayan

I managed to do it, but somehow it doesn’t look elegant at all. For reliability, I would like to look for comparison by anchor or bypass some built-in function to eliminate the cycle

$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;
                                        }
                                    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question