categories and sub categories wordpress - php

I have created some custom categories for products and it has sub categories and the sub categories has further sub categories. now first i display the main categories. If i display there sub then all the sub categories related to that category and its sub category are displayed.I want to show them step by step.That is if the user click on main category then it goes to its sub category page. If user click one of its sub category then it should go to sub categories and if has no sub category then displayed the products. Code is this
$products = get_term_children($term_id[0], 'product-cat');
if(count($products) > 0){
$count = 0;
$sorted_products = array();
foreach ($products as $product) {
$sorted_products = get_term($product, 'product-cat');
$prod_meta = get_option("taxonomy_term_".$term->term_id);
//echo "<pre>"; print_r($sorted_products);
foreach ($sorted_products as $product) { ?>
<div class="col-md-3 col-sm-4 col-xs-12">
<a href="<?php echo $product['link']; ?>">
<a href="<?php echo $product['link']; ?>" class="hvr-grow">
<img class="center-block img-responsive" src="<?php echo $product['img'] ? $product['img'] : '/wp-content/themes/ruskin/images/dummy-product.jpg'; ?>" alt="<?php echo $product['name']; ?>">
<h3><?php echo $product['name']; ?></h3>
else{
# Define the WP Query post arguments.
$args = array(
'post_status' => 'publish',
'post_type' => 'products',
'posts_per_page' => -1,
//'meta_query' => array('relation' => 'AND', array('key' => '_cus__featured', 'value' => '1', 'compare' => '='),),
'meta_key' => '_cus__sort_order',
//'meta_value' => 'meta_value',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'tax_query' => array(
array('taxonomy' => 'product-cat',
'field' => 'slug',
'terms' => $cats
)));
$loop = new WP_Query($args);
$total = $loop->found_posts;
$sliders='';
// Generatet the slider conteents
while ($loop->have_posts()) {
$loop->the_post();
$listingimg = get_post_custom_values('_cus__listing_img');
$listingimg = "/wp-content/themes/bodyo/images/no-slider-img.jpg";
$img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'main_slide_img');
$img = "/wp-content/themes/bodyo/images/no-slider-img.jpg";
$sliders .= '<a href="'. get_the_permalink() .'" class="hvr-grow">';
$sliders .= '<img src="'.$listingimg.'" class="center-block img-responsive" alt="'. get_the_title() .'" />';
$sliders .= '</a>';
$sliders .= '</div>';
$sliders .= '<a href="'. get_the_permalink() .'">';
$sliders .= '<h3>'. get_the_title() .'</h3>';
$sliders .= '<p>'. get_the_excerpt() .'</p>';
$sliders .= 'read more';
$counter++;
}
It over-rites the previous sort order. That is if from dashboard we give 2 to three categories in sort order then it displays just the last one. First two are overwritten.

Something like this should do it.
$args = array(
'child_of' => $term_id[0],
'taxonomy' => 'product-cat',
'hierarchical' => true,
'depth' => 1,
);
$categories = get_categories($args);
Keep in mind that get_categories() is a wrapper of get_terms().
You can find all the accepted values here but the one you're looking for is depth.

Related

Display Woocommerce product categories with child and product number in grid

I have to display product categories grid, where a single column will look like this:
child category
main category
number of products
I am trying to do this with the following code. Instead of "main category" I get the "child" name, which is the same in all cases.
<?php
$taxonomy = 'product_cat';
$parent_cat_ids = get_terms( $taxonomy, array(
'parent' => 0,
'hide_empty' => false,
'fields' => 'ids'
) );
$subcategories = get_terms( $taxonomy, array(
'exclude' => $parent_cat_ids,
'orderby' => 'name',
'order' => 'asc',
'hide_empty' => false,
) );
$category_id = $cat->term_id;
if( ! empty( $subcategories ) ){
echo '<ul>';
foreach ($subcategories as $subcategory) {
echo '<li>
<h4><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</h4>
<a href="'. get_term_link($subcategory) .'" >' . $subcategory->name.'</a>
</li>';
echo ' ('.$cat->count.')';
}
echo '</ul>';
}
?>

Show Post Count on Subcategories of a Custom Post Type

I have a custom post type called products with product_categories as its taxonomy name. It contains categories which each have subcategories and posts.
I want to display a count of all posts in the subcategories only.
Here is what I have so far:
<?php
// POSTS LOOP
// Query
$args = array( 'post_type' => 'products' , 'orderby' => 'menu_order', 'order' => 'ASC' ,
'tax_query' => array(
array(
'taxonomy' => 'product_categories',
'terms' => array(14),
'include_children' => false
)
));
$query = new WP_Query( $args );
// Post Counter
$term_id = 14;
$tax_name = 'product_categories';
$terms = get_terms($tax_name, array('parent' => 0));
$term_children = get_term_children($term_id,$tax_name);
$post_count = 0;
// Loop Structure
echo '<ul>';
while ( $query->have_posts() ) : $query->the_post();
echo '<li class="zazzoo-page">';
foreach($terms as $term) {
$term_children = get_term_children($term->term_id,$tax_name);
echo '<ul>';
foreach($term_children as $term_child_id) {
$term_child = get_term_by('id',$term_child_id,$tax_name);
$post_count = $term_child->count;
echo '<div class="zazzoo-counter">'. $post_count .' Designs</div>';
}
echo '</ul>';
} ?>
<div class="title-border"><div class="page-title"><?php the_title(); ?></div></div>
<div class="hovereffect">
<?php the_post_thumbnail('zazzoo-thumb', array('class' => 'responsive-img')); ?>
<div class="overlay">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
<?php echo '</li>';
endwhile;
echo '</ul>';
wp_reset_query();
?>
Above is an image of what it currently looks like. Brochures and Card Tags (in which there are 4 posts each) are subcategories that the post count value should display on and the rest are posts.
I'm still learning WordPress theme development so if anyone can offer some guidance, it would be greatly appreciated.
If you want posts and terms mixed with posts and show them together - that is a pretty hard task.
I would advise you to only use one or the other in a list - much easier.
To get to that you may do the following:
Create a category for all of your posts you want to show (e.g. create "Floor graphics", "Mugs", "Notepads" and "Stickers"), like you did for "Brochures" and "Card Tags". This way you'll have 2 subcategories with 4-4 posts in it, and 4 subcategories with 1-1 post in it.
If you do this, then you only have to use the get_terms() query.
This could be your query (where you want to see subcategories of a term with ID 14):
$tax_name = 'product_categories';
$parent_id = 14;
$terms = get_terms( $tax_name, array( 'parent' => $parent_id, 'pad_counts' => 1, ) );
Then you can go, and print it:
$html = '';
$html .= '<ul>';
foreach ( $terms as $single_term ) {
$html .= '<li class="zazzoo-page">';
// only put the tag on terms, that have more than 1 items in it
if ( $single_term->count > 1 ) {
$html .= '<div class="zazzoo-counter">'. $single_term->count .' Designs</div>';
}
$html .= '<div class="title-border">';
$html .= '<div class="page-title">';
$html .= $single_term->name;
$html .= '</div>';
$html .= '</div>';
$html .= '<div class="hover effect">';
$html .= '<div class="overlay">';
$html .= $single_term->name;
$html .= '</div>';
$html .= '</div>';
$html .= '</li>';
}
$html .= '</ul>';
echo $html;
This way you will have the list with names and counts (where needed), but you will lose the image and the content (on hover).
If you want the image and the content back, you may query the first post of each subcategory for this content, like this:
$posts_array = get_posts(
array(
'posts_per_page' => 1,
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'product_categories',
'field' => 'term_id',
'terms' => $single_term->term_id,
)
)
)
);
OR you can assign an image and / or a description to the custom taxonomy subcategories you display.
I hope this helps you out a bit. :)

WordPress permalink to product

I am listing out product categories on my website but for some reason the permalink to the product inside the loop is just leading the user to a blog post and not to the product or category etc:
<div class="mobile-show">
<?php
$args = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_categories = get_terms( 'product_cat', $args );
foreach( $product_categories as $cat ) { ?>
<?php echo '<ul class="cat_list_mobile">'; ?>
<a href="<?php echo get_permalink(); ?>">
<?php echo '<li><div class="col-group-2">' . $cat->name . '</div><div class="col-group-2 text-right"><i class="fa fa-chevron-right"></i></div></li>
</a>
</ul>';
}
?>
</div>
It is displaying the categories as expected, maybe I am missing something here?
If you need to get permalink of the product category then use get_category_link( $category_id );
More in the codex: https://codex.wordpress.org/Function_Reference/get_category_link

Loop through categories and create tab for each Wordpress and Bootstrap 3

I'm trying to create a page that holds all categories of my custom post type as tabs, with a tab content.
I am able to display all the category names as tabs, but i need to run a query in each tab content area to the corresponding category.
So when I click on tab named "1" the tab content area should only show posts from the category belonging to the tab named "1".
My code so far:
<?php
echo '<ul class="nav nav-tabs" role="tablist">';
$args = array(
'hide_empty'=> 1,
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<li><a href="#' . $category->name.'" role="tab" data-toggle="tab">' .
$category->name.'</a></li>';
$cat_name = $category->name;
}
echo '</ul>';
echo '<div class="tab-content">';
foreach($categories as $category) {
echo '<div class="tab-pane" id="' . $category->name.'">';
?>
<?php
$the_query = new WP_Query(array(
'post_type' => 'acme_product',
'posts_per_page' => 100,
'category_name' => $cat_name
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<h1><?php the_title(); ?></h1>
<?php
endwhile;
?>
<?php }
echo '</div>';
?>
The problems is that each content area displays all post of every category.
What i want to achieve: Link
Any suggestions?
'category_name' parameter takes value as category slug. So you should use category slug in place of category name in the query.
$cat_slug = $category->slug;
$the_query = new WP_Query(array(
'post_type' => 'acme_product',
'posts_per_page' => 100,
'category_name' => $cat_slug
));
i asked for this problem in previous time but after some attempt for solving my problem i solved it by this perfect cod that work 100%100 with me
<?php
echo '<ul class="nav nav-tabs" role="tablist">';
$args = array(
'hide_empty'=> 1,
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo
'<li>
<a href="#'.$category->slug.'" role="tab" data-toggle="tab">
'.$category->name.'
</a>
</li>';
}
echo '</ul>';
echo '<div class="tab-content">';
foreach($categories as $category) {
echo '<div class="tab-pane" id="' . $category->slug.'">';
$the_query = new WP_Query(array(
'post_type' => 'acme_product',
'posts_per_page' => 100,
'category_name' => $category->slug
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<h1>';
the_title();
echo '</h1>';
endwhile;
}
echo '</div>';
?>

WORDPRESS Simple Query

I have a category query, and in my category query I want to get product (only one) by queried category id (or name or whatsoever)
I start query:
<?wpsc_start_category_query(array('category_group'=> get_option('wpsc_default_category'))); ?>
and then try to use get_posts() function to get product:
$args = array(
'post_type' => 'wpsc-product',
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'wpsc_product_category',
'field' => 'id',
'terms' => $aka
)));
$cat1_posts = get_posts($args);
where $aka is:
$aka = '[wpsc_category_id]';
but when I echo $cat1_posts[0]->ID; it only shows my last product ID for every category. what is the problem? echoing only [wpsc_category_id] works perfect.
I tried EVERYTHING for the last few days. I will buy you cookies for help
I've got to idea, that I need foreach or anything like this
You can use the get_terms() function. So something like this (untested)
<?php
//for each category, show latest post
$cat_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_terms( 'wpsc_product_category');
foreach($categories as $category) {
$args=array(
'showposts' => 1,
'post_type' => 'wpsc-product',
'wpsc_product_category' => array($category->slug)
);
$posts=get_posts($args);
if ($posts) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post); ?>
<p><?php the_title(); ?></p>
<?php
} // foreach($posts
} // if ($posts
} // foreach($categories
?>

Categories