Show subcategory instead of parent category - wordpress

I am using the code below for displaying the WordPress post category but it outputs the parent category, not the subcategory. Can this be modified to show the subcategory instead?
<div class="category">' . get_category(get_query_var('cat'))->name . '</div>

use this code to get sub-categories by ID
<?php $cats = get_the_category($post->ID);
$sep = '';
foreach( $cats as $cat ) {
$subcats = get_categories('child_of='.$cat->term_id);
if($subcats) {
foreach( $subcats as $subcat )
{ echo $sep . $subcat->name; $sep = ', '; }
}
}
?>

if you want to show list of WordPress category then use this code
<div class="category"> <?php _e('Categories:'); ?> <?php wp_list_cats(); ?></div>
OR you want to call woocomerce category then call use this code
<div class="category">
<?php
$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 );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br />'. $cat->name .'';
$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) {
echo $sub_category->name ;
}
}
}
}
?>
</div>

Related

Woocommerce how to get product category

I want to create a drop down list for categories in php in a page built in code. I could create a shortcode for it successfully but since shortcode is not friendly with html, I decided not to pursue that route. So without any scripts or jquery, I'm attempting to add php code into a div
<select class="store-search-input form-control" name="dokan_seller_search" value="<?php
$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 );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br />'. $cat->name .'';
$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) {
echo $sub_category->name ;
}
}
}
}
?>" >
</select>
and this happened:
Any help would be appreciated
Update thanks to Alex's help:
I don't have enough points to add the image directly yet:
<input type="select" class="store-search-input form-control" name="dokan_seller_search" value="<?php
$args = array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'hierarchical' => true,
'hide_empty' => false
);
$all_categories = get_categories( $args );
esc_html('<select class="store-search-input form-control" name="dokan_seller_search">');
foreach($all_categories as $parent){
if ($parent->category_parent == 0) {
echo '<option value="'.$parent->name.'"><a href="'. get_term_link($parent->slug, 'product_cat') .'">'. $parent->name .'</option>';
}
$args2 = array(
'taxonomy' => 'product_cat',
'child_of' => $parent->term_id,
'parent' => $parent->term_id,
'hide_empty' => 'false'
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name;
}
}
}
esc_html('</select>');
?>">
The result I'm expecting is a
What you are trying to do is a little dirty for a . You would be much better off using a dropdown triggered by javascript so you can better control the DOM in the dropdown. A select should really only be used when you need to submit data to the server.
UPDATE: This is tested and working.
<script type="text/javascript">
//First, some js to trigger the window to relocate when you click an option
jQuery(document).ready(function(){
//When the select is triggered
jQuery('#category-links').on('click', function(){
//Get the URL from the selected option
var url = jQuery(this).val();
//Navigate to the url
window.location.replace(url);
})
})
</script>
<?php
//Get the parent terms
$parents = get_terms([
'taxonomy' => 'product_cat',
'hide_empty' => false,
'parent' => 0
]);
//Start the dom for the select
echo '<select id="category-links">';
echo '<optgroup>';
//The parent option
echo '<option value="'.get_term_link($term->term_id).'">'. $term->name .'</option>';
//Check for children, if there are we'll add them here
$children = get_term_children($term->term_id, 'product_cat');
if (!empty($children)) {
foreach ($children as $child) {
$childterm = get_term_by('id', $child, 'product_cat');
echo '<option value="'.get_term_link($childterm->term_id).'">'. $childterm->name .'</option>';
}
}
echo '</optgroup>';
}
echo '<select>';

How to display the sub subcategory in woocommerce wordpress

I am trying to get the product categories of subcategory of subcategory
<?php
$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 );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$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 ) {
echo '<li class="title '.$cat->name.'"><a >
';
echo $cat->name ;
echo '<i class="chevron right icon"></i>
</a></li>';
$sub_cats = "";
}else {
echo '<li class="titleVide '.$cat->name.'Hide"><a >
';
echo $cat->name ;
echo '</a></li>';
$sub_cats = "";
}
$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) {
echo '<li class="content"> <p>';
foreach($sub_cats as $sub_category) {
echo '
'. $sub_category->name . '
';
}
echo ' </p></li>';
} else {
echo '';
}
$sub_cats = "";
}
} /* end foreach all_categories cat */
wp_reset_query();
?>
This code list all the top level categories and subcategories under them hierarchically, but i have subcategories of subcategory(sub-sub-category), so how can i list those sub subcategories(with clic).
Categories IN BO
List categories in FO
try this below code by using child_of = current category id
<?php
$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 );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$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 ) {
echo '<li class="title '.$cat->name.'"><a >
';
echo $cat->name ;
echo '<i class="chevron right icon"></i>
</a></li>';
$sub_cats = "";
}else {
echo '<li class="titleVide '.$cat->name.'Hide"><a >
';
echo $cat->name ;
echo '</a></li>';
$sub_cats = "";
}
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => $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) {
echo '<li class="content"> <p>';
foreach($sub_cats as $sub_category) {
echo '
'. $sub_category->name . '
';
}
echo ' </p></li>';
} else {
echo '';
}
$sub_cats = "";
}
} /* end foreach all_categories cat */
wp_reset_query();
?>
or use wp_list_categories() function for listing category
https://developer.wordpress.org/reference/functions/wp_list_categories/

How to call category in dashboard if mine posted is attached to them only?

I am making user dashboard and I create a dashboard widget for category. Now I want to show category if current user's post is attached to them.
Currently I am able to see all categories.
Here is my function
function user_categories() {
wp_add_dashboard_widget(
'wp_widget_category', // Widget slug.
'Categories', // Title.
'my_dashboard_category' // Display function.
);
function my_dashboard_category() {
if ( is_user_logged_in() ):
$current_user = wp_get_current_user();
if ( ($current_user instanceof WP_User) ) {
?>
<?php
$args = array(
'type' => 'recipes',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'author' => $current_user->ID,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'recipe_categories',
'pad_counts' => false );
$categories = get_categories($args);
foreach ($categories as $category) {
$url = get_term_link($category);?>
<div class="submitted_recipe">
<a href="<?php echo $url;?>"><?php echo do_shortcode(sprintf('[wp_custom_image_category size="large_blog" term_id="%s"]',$category->term_id)); ?>
<h2><?php echo $category->name; ?></h2>
</a>
</div>
<?php
}
?>
<?php
}
endif;
}
}
add_action( 'wp_dashboard_setup', 'user_categories' );
You need to query all user posts in each categorie, if it's not empty then include this category. try this revised code
function user_categories() {
wp_add_dashboard_widget(
'wp_widget_category', // Widget slug.
'Categories', // Title.
'my_dashboard_category' // Display function.
);
function my_dashboard_category() {
if ( is_user_logged_in() ):
$current_user = wp_get_current_user();
if ( ($current_user instanceof WP_User) ) {
?>
<?php
$args = array(
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'recipe_categories',
'pad_counts' => false );
$categories = get_categories($args);
foreach ($categories as $category) {
$posts_query = new WP_Query( array(
'post_type' => 'recipes',
'author' => $current_user->ID,
'tax_query' => array(
array('taxonomy' => 'recipe_categories',
'field' => 'slug',
'terms' => $category->slug)
),
'fields' => 'ids'
) );
$ids = $posts_query->posts;
if (!empty ($ids)) {
$url = get_term_link($category);?>
<div class="submitted_recipe">
<a href="<?php echo $url;?>"><?php echo do_shortcode(sprintf('[wp_custom_image_category size="large_blog" term_id="%s"]',$category->term_id)); ?>
<h2><?php echo $category->name; ?></h2>
</a>
</div>
<?php
}
}
?>
<?php
}
endif;
}
}
add_action( 'wp_dashboard_setup', 'user_categories' );

display woocommerce category list

How can I get the category list in woocommerce?
With this code, I get wordpress category list:
function gaga_lite_category_lists(){
$categories = get_categories(
array(
'hide_empty' => 0,
'exclude' => 1
)
);
$category_lists = array();
$category_lists[0] = __('Select Category', 'gaga-lite');
foreach($categories as $category) :
$category_lists[$category->term_id] = $category->name;
endforeach;
return $category_lists;
}
I want to replace it with woocommerce category.
WooCommerce Product Category are treated as product_cat taxonomy
Here is the code.
function gaga_lite_category_lists()
{
$category_lists = array();
$category_lists[0] = __('Select Category', 'gaga-lite');
$args = array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'hierarchical' => 0, // 1 for yes, 0 for no
'hide_empty' => 0,
'exclude' => 1 //list of product_cat id that you want to exclude (string/array).
);
$all_categories = get_categories($args);
foreach ($all_categories as $cat)
{
if ($cat->category_parent == 0)
{
$category_lists[$cat->term_id] = $cat->name;
//get_term_link($cat->slug, 'product_cat')
}
}
return $category_lists;
}
$orderby = 'name';
$order = 'asc';
$hide_empty = false ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
echo '<ul>';
foreach ($product_categories as $key => $category) {
echo '<li>';
echo '<a href="'.get_term_link($category).'" >';
echo $category->name;
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
You can get all Woocommerce categories and sub categories using below code:
$taxonomy = 'product_cat';//Woocommerce taxanomy name
$orderby = 'name';
$show_count = 0; //set 1 for yes, 0 for no
$pad_counts = 0; //set 1 for yes, 0 for no
$hierarchical = 1; //set 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
);
//get all woocommerce categories on the basis of $args
$get_all_categories = get_categories( $args );
foreach ($get_all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br />'. $cat->name .'';
//Create arguments for child category
$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
);
//Get child category
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
}
}
}
I hope it will help you. Thanks

woocommerce breadcrumbs show parent and sub categories?

I'm having a really hard time with this... essentially I just want to display breadcrumbs on the product category pages in woocommerce so that it shows the main category and sub categories. Hoping some smart kind soul can help me :)
This normally could be done easily with the following code:
<?php woocommerce_breadcrumb(); ?>
However, I'm using a purchased theme with woocommerce integrated, so it doesn't have that function and I tried to integrate it but no luck.
So, I found someone who had the same issue and made my own breadcrumbs for prordu. Which works, but I just want the product category to show ONLY the main category and sub category each product have. The code shows ALL categories and sub categories, not just the one I want to display.
<?php
$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
);
?>
<?php $all_categories = get_categories( $args );
//print_r($all_categories);
foreach ($all_categories as $cat) {
//print_r($cat);
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
?>
<?php
echo '<br />'. $cat->name .''; ?>
<?php
$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) {
echo $sub_category->name ;
}
} ?>
<?php }
}
?>

Resources