questions

Get product tags in Slider Revolution, WordPress

Rate this post

I did some custom code to create a drop-down button, but the menu items are static. I want to get all product tags and to place them inside my menu. (Name + slug). Default Slider Revolution has {{wc_tags}}, but I don’t get any output from this one. Extra: I also user Tag Groups, and I want to get tags only from one category (but that’s just extra).

<ul id="menu">
  <li id="menu_button" >
        <span> I could use support with...</span>
        <i class="fas fa-chevron-down"></i>
        <ul id="drop-down">  
        </ul> 
  </li> 
</ul>

<script>var data = {
        "menu":[
        {"name": "Node 1", "link":"{{current_page_link}}private/?product_tag_1=anxiety-and-depression"},
        {"name": "Node 2", "link":"#"},
        {"name": "Node 3", "link":"#"},
    ]
};

function parseMenu(element, menu) {
    for (var i=0; i<menu.length; i++) {
        var nestedli = document.createElement('li');
        nestedli.setAttribute('style', 'display:none;');
        nestedli.setAttribute('onmouseenter', "showNodes(this)");
        nestedli.setAttribute('onmouseleave', "hideNodes(this)");
    var link = document.createElement('a');
    link.setAttribute('href', menu[i].link);
    link.appendChild(document.createTextNode(menu[i].name));
        nestedli.appendChild(link);
        if (menu[i].submenu!=null) {
            var subul = document.createElement('ul');
            nestedli.appendChild(subul);
            parseMenu(subul, menu[i].submenu);
        }
        element.appendChild(nestedli);
    }
}

function showNodes(element){
    var menu = document.getElementById('drop-down');
    menu.style.display = "block";
    var lis = element.querySelectorAll("ul > li");
    for (var i=0;i<lis.length;i++) {
        lis[i].style.display = "block";
    }  
}

function hideNodes(element){
    var lis = element.querySelectorAll("ul > li");
    for (var i=0;i<lis.length;i++) {
        lis[i].style.display = "none";
    }  
}

function showMenu(){
    var menu = document.getElementById('drop-down');
    menu.style.display = "block";
    var lis = document.querySelectorAll("#drop-down > li");
    for (var i=0;i<lis.length;i++) {
        lis[i].style.display = "block";
    }  
}

 function hideMenu(){
    var menu = document.getElementById('drop-down');
    menu.style.display = "none";
}

window.onload=function(){
    var menu= document.getElementById('drop-down');
    parseMenu(menu, data.menu);
}; 
</script>

 

✔️Solution:

I create a shortcode from where I get all tags from a specific category.

"menu":[get_wc_tags]

function get_woocommerce_tags(){
  $terms = get_terms(array('taxonomy' => 'product_tag', 'hide_empty' => false));
  $sorted_terms = [];

  foreach ( $terms as $term ) {
    $term->link = 'my_url' . $term->slug;
    $term_meta = '';
    $term_meta = get_term_meta( $term->term_id, '_cm_term_group_array', true );

    $term_meta_array = explode(',', $term_meta);


    if(in_array('1', $term_meta_array)){
      $term->term_group = 1;
      array_push( $sorted_terms, $term);
    }

  }

  return  json_encode($sorted_terms);
}

add_shortcode( 'get_wc_tags', 'get_woocommerce_tags' );

Leave a Reply

Your email address will not be published.

Back to top button