Skip to content

Instantly share code, notes, and snippets.

@chadsten
Forked from JakeKalkman/sidebar.php
Created January 19, 2019 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadsten/398fc479863bff6bfe633e8e36b9caa5 to your computer and use it in GitHub Desktop.
Save chadsten/398fc479863bff6bfe633e8e36b9caa5 to your computer and use it in GitHub Desktop.
<?php
function tag_sidebar(){
$taxonomy = 'product_cat';
$orderby = 'name';
$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 "<ul class=" . "'sidebar-parent-list'". ">";
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$tag = get_queried_object();
$modname = str_replace(" ","-", strtolower($cat->name));
$category_id = $cat->term_id;
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats)
{
foreach($sub_cats as $sub_category) {
if($tag->slug == "earrings")
{
if(strpos($sub_category->slug, "earring") == true)
{
echo "<li class=" . "'sidebar-list-items'" . ">" .'<a href="'.
get_term_link($sub_category->slug, 'product_cat') .'">'. $cat->name .'</a>'. "</li>";
}
}
elseif($tag->slug == "ring" ||$tag->slug == "bracelet" || $tag->slug == "necklace" )
{
if(strpos($sub_category->slug, $tag->slug) !== false && strpos($sub_category->slug, "earring") != true)
{
echo "<li class=" . "'sidebar-list-items'" . ">". '<a href="'.
get_term_link($sub_category->slug, 'product_cat') .'">'. $cat->name .'</a>' . "</li>";
}
}
}
}
}
}
echo "</ul>";
}
add_shortcode('tagsidebar','tag_sidebar');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment