michael.mcghee of Papercut Interactive
10/12/2018 - 8:00 PM

WP Category Dropdown (GrainCraft version)

This is the GrainCraft version of the category listing dropdown.

{% if menu %}
	<ul>
	{% for item in menu %}
		<li class="{{ item.classes | join(' ') }}">
			<a target="{{ item.target }}" href="{{ item.link }}">{{ item.title }}</a>
			{% if item == "Products" %}
				{% include 'category-listing.twig' %}
			{% else %}
				{% include "menu.twig" with {'menu': item.get_children} %}
			{% endif %}
		</li>
	{% endfor %}
	</ul>
{% endif %}
{{ function('get_taxonomy_and_post') }}
<?php

function get_taxonomy_and_post() {
    $tax = 'product-categories'; // Your Taxonomy, change it if you not using wordpress native category
    $terms = get_terms( $tax ,array( // get all taxonomy terms
        'orderby'    => 'name',
        'order'      => 'ASC',
        'hide_empty' => 0,
    ));



    //Loop throug each taxonomy terms,
		echo '<ul class="submenu"><li><a href="/all-products">All Products</li>';
    foreach ( $terms as $term ) {

        //Query argument for post
        $args = array(
            'post_type' => 'products', // Or Custom Post Type,
            'order' => 'DESC',
            'orderby' => 'date',
            'taxonomy' => $tax,
            'term' => $term->slug, // Query posts for each term based on term slug
        );
        $query = new WP_Query( $args );
        $posts = $query->get_posts();

        //echo '<li><a href="'.$term->name.'</li>';
				echo '<li><a href="'.get_term_link($term) /**use get_permalink( $post->ID ) if you want the custom permalink**/.'">'.$term->name.'</a>';
        if ( $posts ) {
					echo "<ul>";
            foreach ( $posts as $post ) {
							//$post->guid
                echo '<li><a href="'.get_permalink( $post->ID ) /**use get_permalink( $post->ID ) if you want the custom permalink**/.'">'.$post->post_title.'</a></li>';
            }
						echo "</ul>";
        }
    }
		echo "</li></ul>";
}

?>


add_action('wp_head', 'get_taxonomy_and_post');