Skip to content

Instantly share code, notes, and snippets.

@dasbairagya
Created April 28, 2017 11:57
Show Gist options
  • Select an option

  • Save dasbairagya/0e5275db39e4fbe2a8e9b8692a76c5d6 to your computer and use it in GitHub Desktop.

Select an option

Save dasbairagya/0e5275db39e4fbe2a8e9b8692a76c5d6 to your computer and use it in GitHub Desktop.
Get the image,title and link of the parent term of product_cat taxonomy in woocommerce for creating the slide in home page with parent category.
<?php
$pterms = get_terms( array(
'taxonomy' => 'product_cat',
'parent' => 0
) );
// echo '<pre>';
// var_dump($terms);
foreach ($pterms as $myterm) {
$term_link = get_term_link( $myterm );
$thumbnail_id = get_woocommerce_term_meta($myterm->term_id, 'thumbnail_id', true);
$url = wp_get_attachment_url($thumbnail_id);//print the image
$link = esc_url( $term_link );//print the link
$name = $myterm->name;//print the name of the parent term
// var_dump($url);
?>
//your html code here
<?php } ?>//end for each
@dasbairagya
Copy link
Author

dasbairagya commented May 24, 2017

To get all the categories and and subcategories you can use the below code....
<?php
$taxonomy = 'product_cat';
$orderby = 'field';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;

			$args = array(
			'taxonomy' => $taxonomy,
			'orderby' => $orderby,
			'show_count' => $show_count,
			'pad_counts' => $pad_counts,
			'hierarchical' => $hierarchical,
			'title_li' => $title,
			'hide_empty' => $empty
			);

$all_categories = get_categories( $args );
echo "

    ";
    foreach ($all_categories as $cat){
    $term_link = get_term_link( $cat );
    $link = esc_url( $term_link );
    ?>

    <li>
    	<a href="<?php echo $link;?>" title="<?php echo esc_attr($cat->name ? $cat->name : $cat->term_id); ?>"><?php echo $cat->cat_name; ?>
    	</a>
    </li>
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment