Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

jasperg on "adding mark up to woo commerce product categories"

$
0
0

I have been trying to style my product category list but to do so means I need to add a some mark up to them.

Ok just to sum up what I want to achieve seeing as I have been doing this for a week and maybe on completely the wrong path.

I want to add classes to current category and current category ancestors
I would also like to add mark up to the < a> tags as well. I have been reading about "walkers" all week and still got almost no clue.

I a using woo commerce and I can get it working by adding the mark up to
class-product-cat-list-walker.php

public function start_el( &$output, $cat, $depth = 0, $args = array(), $current_object_id = 0 ) {
		$output .= '<li class="cat-item cat-item-' . $cat->term_id;

		if ( $args['current_category'] == $cat->term_id ) {
			$output .= ' current-cat';
		}

		if ( $args['has_children'] && $args['hierarchical'] ) {
			$output .= ' cat-parent';
		}

		if ( $args['current_category_ancestors'] && $args['current_category'] && in_array( $cat->term_id, $args['current_category_ancestors'] ) ) {
			$output .= ' current-cat-parent';
		}

		$output .=  '"><a href="' . get_term_link( (int) $cat->term_id, 'product_cat' ) . '" class="btn btn--small  btn--full  catagory-menue__btn"  >'  . __( $cat->name, 'woocommerce' ) .  '</a>';

		if ( $args['show_count'] ) {
			$output .= ' <span class="count">(' . $cat->count . ')</span>';
		}
	}

I added my mark up and it works. But it is not ideal as then I cannot update easily . I tried copying the code for the whole walker to my functions.php file on my theme and changed

class WC_Product_Cat_List_Walker extends Walker
to
class Book_Cat_Btn extends Walker
to keep it form getting confused. This worked! which was amazing till I realised I no longer have some of the classes that woo commerce had added to my category list.

it seems to me that it all works apart from

if ( $args['current_category_ancestors'] && $args['current_category'] && in_array( $cat->term_id, $args['current_category_ancestors'] ) ) {
			$output .= ' current-cat-parent';
		}

Is there any way I can get this to work? I assumed current_category_ancestors was part of WordPress code but it seems not and I have no clue how to replicate what ever is making it work.


It looks like this may do what I want but I am having trouble working it out

thanks for any help anyone can give me. Even a online lesson or a book i can get that would tell me what is going on, I just completed code academy PHP and it helped but i am still at the stage where I don't even know what to ask most of the time


Viewing all articles
Browse latest Browse all 8245

Trending Articles