Skip to content

Instantly share code, notes, and snippets.

@vishalbasnet23
Created September 2, 2016 03:51
Show Gist options
  • Save vishalbasnet23/45c3d20b914d57419e93a6594bf6156b to your computer and use it in GitHub Desktop.
Save vishalbasnet23/45c3d20b914d57419e93a6594bf6156b to your computer and use it in GitHub Desktop.
Get post drop down with category.
<?php
function category_terms_with_post_builder( $tax_name = 'product_cat', $post_type = 'product' ) {
$parent_terms = get_terms( $tax_name, array(
'hide_empty' => false,
'fields' => 'ids',
'parent' => 0
) );
$parent_terms_with_keys = array_combine($parent_terms, $parent_terms);
foreach( $parent_terms_with_keys as $terms_key => $terms_value ) {
$child_terms = get_terms( $tax_name, array(
'hide_empty' => false,
'fields' => 'ids',
'parent' => $terms_value
));
$parent_terms_with_keys[$terms_key] = $child_terms;
}
foreach( $parent_terms_with_keys as $parent_terms_key => $parent_child_terms ) {
if( 0 < count( $parent_child_terms ) ) {
$new_child_array = array_combine( $parent_child_terms, $parent_child_terms );
$parent_terms_with_keys[$parent_terms_key] = $new_child_array;
foreach( $new_child_array as $child_key => $child_value ) {
if( $post_type == 'post' ) {
$tax_query_args = array( 'cat' => $child_value, 'posts_per_page' => -1 );
} else {
$tax_query_args = array(
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => $tax_name,
'field' => 'id',
'terms' => $child_value
)
),
'posts_per_page' => -1
);
}
$tax_query_posts = get_posts( $tax_query_args );
$this_cat_posts = array();
foreach( $tax_query_posts as $posts ) {
array_push($this_cat_posts, $posts->ID);
}
$parent_terms_with_keys[$parent_terms_key][$child_value] = $this_cat_posts;
}
} else {
if( empty($parent_terms_with_keys[$parent_terms_key]) ) {
if( $post_type == 'post' ) {
$tax_query_args = array( 'cat' => $parent_terms_key, 'posts_per_page' => -1 );
} else {
$tax_query_args = array(
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => $tax_name,
'field' => 'id',
'terms' => $parent_terms_key
)
),
'posts_per_page' => -1
);
}
$empty_tax_query_posts = get_posts( $tax_query_args );
$this_cat_posts = array();
foreach( $empty_tax_query_posts as $posts ) {
array_push($this_cat_posts, $posts->ID);
}
$parent_terms_with_keys[$parent_terms_key]['skipthis'] = $this_cat_posts;
}
}
}
return $parent_terms_with_keys;
}
function custom_menu_builder( $tax_name = 'product_cat', $post_type = 'product' ) {
$menu_object = category_terms_with_post_builder( $tax_name, $post_type );
$menu_html = '<ul class="sub-menu">';
foreach( $menu_object as $grand_parent_key => $grand_parent_value ) {
$menu_html .= '<li class="menu-item-has-children">';
$menu_html .= '<a href="javascript:void(0);">'.get_term($grand_parent_key, $tax_name)->name.'</a>';
$menu_html .= '<ul class="sub-menu">';
if( ! array_key_exists( 'skipthis', $grand_parent_value ) ) {
foreach( $grand_parent_value as $grand_children_key => $grand_children_value ) {
$menu_html .= '<li class="first-child menu-item-has-children">';
$menu_html .= '<a href="javascript:void(0);">'.get_term( $grand_children_key, $tax_name)->name.'</a>';
$menu_html .= '<ul class="sub-menu">';
if( !empty( $grand_children_value) ) {
foreach( $grand_children_value as $last_child_value ) {
$menu_html .= '<li class="last-child">';
$menu_html .= '<a href="'.get_the_permalink($last_child_value).'">'.get_post($last_child_value)->post_title.'</a>';
$menu_html .= '</li>';
}
}
$menu_html .= '</li>';
$menu_html .= '</ul>';
}
} else {
foreach( $grand_parent_value as $post_ids ) {
foreach( $post_ids as $post_id ) {
$menu_html .= '<li class="last-child">';
$menu_html .= '<a href="'.get_the_permalink($post_id).'">'.get_post($post_id)->post_title.'</a>';
$menu_html .= '</li>';
}
}
}
$menu_html .= '</ul>';
$menu_html .= '</li>';
}
$menu_html .= '</ul>';
echo $menu_html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment