How to show category hierarchy in woocommerce
What i want is something like this
given with this slug
domain.com/category/balloons/helium-baloons
the hierarchy would be:
Baloons
Helium Baloons
Decor
Weddings
To simplified the idea it should be something like this
you can use wp_list_categories function with arguments
wp_list_categories(
array(
'taxonomy'=>product_cat'
)
);
use the below code to achive this functionality
$myterms = get_terms( 'category', array( 'parent' => 0 ) );
$current_term = get_queried_object();
echo '<ul>';
foreach($myterms as $term){
echo '<li> '.$term->name.'';
if($term->term_id == $current_term->parent){
$child_terms = get_terms( 'category', array('child_of'=>$term->term_id) );
if($child_terms){
echo '<ul>';
foreach($child_terms as $child_term){
echo '<li> '.$child_term->name.'';
}
echo '</ul>';
}
}
echo '</li>';
}
echo '</ul>';
also if you have big child heirarchi you can make this code recursive.
Related
I'm listing categories for a custom post type, which is going great so far.
The problem is when I get the category link, is linked to a default category archive template, and I'd like to link it to a custom-page-template.
Is there a way to do that?
<?php
//Llamar al catálogo
$taxonomy = 'lookbook-category';
$orderby = 'date';
$post_type = 'post';
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'post_type' => $post_type,
);
?>
<?php
$categorias = get_categories($args);
// Mostrar categorías
foreach ($categorias as $cat) {
echo '<div class="temporadas-catalogo ">';
echo '<h1 class="">'.$cat->name.'</h1>';
echo '<p class="">'.$cat->description.'</p>';
echo '<div class="center margin-top">';
echo '<a class="button " href="'.get_category_link($cat).'">Ver Catálogo</a>';
echo '</div>';
echo '</div>';
}
?>
My assumptions are that you are using a custom Taxonomy called 'lookbook-category', with this you can set a custom taxononmy template like so:
taxonomy-{taxonomy}.php
The Wordpress developer docs is an excellent resource for understanding how the template hierarchy works.
https://developer.wordpress.org/themes/basics/template-hierarchy/
I would like to display a drop down menu for products in a category.
<select>
<option value="CODE HERE">Volvo</option>
</select>
So according to Wordpress coding..
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Okay so I investigated further and I am looking to do a single page template according to https://developer.wordpress.org I am using a child theme for Storefront which is called NOVA WP.
To make this "single page template" I copied page.php and renamed it to page-buildit.php
This is Mypage in which I actually editing the code. I did copy the code but it turns out blank
found this: WooCommerce: Create a shortcode to display product categories
but my undestanding is we cant do this anymore with the new wordpress version.
<?php
$args = array(
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids,
'posts_per_page' =>'-1'
);
$product_categories = get_terms( 'product_cat', $args );
echo "<select>";
foreach( $product_categories as $category ){
echo "<option value = '" . esc_attr( $category->slug ) . "'>" . esc_html( $category->name ) . "</option>";
}
echo "</select>";
?>
Check this out. This is the way to get product categories.!
You can also use the function wp_dropdown_categories to make your code simpler.
To get a dropdown of categories of products you can write like this.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1);
wp_dropdown_categories($args);
Or if you want to hold the output in a variable you can use the argument 'echo'=>0 and then echo the variable to get the same output.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1,
'echo'=>0);
$cats = wp_dropdown_categories($args);
echo $cats;
Okay so here is how I solved it, with the help of Hemnath mouli, I already gave you the credit for the answer but I wanted to publish the products inside a category in a dropbox.
$args = array(
'posts_per_page' => -1,
'product_cat' => 'motherboard',
'post_type' => 'product',
'orderby' => 'title',
);
$products = new WP_Query( $args );
echo "<select>";
foreach ( $products as $product ) {
$products->the_post();
?>
<option value="<?php the_permalink(); ?>"> <?php the_title(); ?>
<?php
}
echo "</select>";
?>
Now I will need to show the image of this product after selecting it.
Firstly, I'm trying to display all tags from a taxonomy called 'group'. However, this taxonomy currently contains two tags, from which one of of them has multiple tagchildren.
Update: I should have mentioned it was for a particular post type.
I'd lik to display all posts belonging to those children. So the final result should look something like this:
Parent Tag A
Child tag
Post data
Child tag
Post data
etc...
"Parent" Tag B
<?php
$taxonomyName = "group";
$terms = get_terms($taxonomyName,array('parent' => 0));
foreach($terms as $term) {
echo ''.$term->name.'';
$term_children = get_term_children($term->term_id,$taxonomyName);
echo '<ul>';
foreach($term_children as $term_child_id) {
$term_child = get_term_by('id',$term_child_id,$taxonomyName);
echo '<li>' . $term_child->name . '</li>';
}
echo '</ul>';
}
?>
May be you should try this...
I hope this will work...
$taxonomyName = "group";
$terms = get_terms($taxonomyName,array('parent' => 0));
echo '<ul>';
foreach($terms as $term)
{
echo '<li>'.$term->name.'';
$term_children = get_term_children($term->term_id,$taxonomyName);
echo '<ul>';
foreach($term_children as $term_child_id)
{
$term_child = get_term_by('id',$term_child_id,$taxonomyName);
echo '<li>' . $term_child->name . '';
echo '<ul>';
$tax_arg = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $taxonomyName,
'field' => 'id',
'terms' => $term_child_id
)
)
);
$posts = get_posts($tax_arg);
foreach($posts as $post)
{
echo '<li>' . $post->post_title . '</li>';
}
echo '</ul>';
echo '</li>';
}
echo '</ul>';
echo '</li>';
}
echo '</ul>';
I'm not sure why this isn't working. I want to exclude categories 406, 982, and 1319 and their children from the list. I don't see any other arguments on wp codex. Does 'exclude' automatically exclude children? Is there another way to do this?
**Edit: I can't even limit the results to 10.
<?php
$args = array(
'exclude' => '406,982,1319',
'#' => '10'
);
$sidebar_artists = get_categories($args);
echo "<ul>";
foreach ($sidebar_artists as $sidebar_artist) {
echo '<li class="cat-item">' . $sidebar_artist->category_nicename . '</li>';
}
echo "</ul>";
?>
Set parent to 0:
<?php
$args = array(
'orderby' => 'name',
'parent' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '' . $category->name . '<br/>';
}
?>
https://codex.wordpress.org/Function_Reference/get_categories
it looks like it should be working but did you try any alternative ? like is_category in if/else? or try exclusion by category sluginstead of IDs for sake of debugging....
WP Codex Reference for is_category
So I have a snippet of code that grabs the categories and their coinciding posts and lists them outside of the loop (Below). I've been trying to get the post to link to #post-[ID] instead of the permalink - but I keep failing. Can anyone help?
<ul id="sidebar">
<?php
foreach( get_categories('orderby=ID&order=desc') as $cat ) :
if( !$cat->parent ) {
echo '<li class="title"><h2>' . $cat->name . '</h2>';
echo '<ul>';
process_cat_tree( $cat->term_id );
}
endforeach;
wp_reset_query(); //to reset all trouble done to the original query
//
function process_cat_tree( $cat) {
$args = array('category__in' => array( $cat ), 'numberposts' => -1);
$cat_posts = get_posts( $args );
$id = $post->ID;
global $post;
if( $cat_posts ) :
foreach( $cat_posts as $menuPost ) :
echo '<li';
if ( $menuPost->ID == $post->ID ) { echo ' class="active"'; }
echo '>';
echo '' . $menuPost->post_title . '';
echo '</li>';
endforeach;
endif;
echo '</ul></li>';
}
?>
The above code is outputting UL/LI tags like this:
CATEGORY
Post
Post
Post
CATEGORY
Post
Post
Post
CATEGORY
Post
Post
Post
Admittedly, I don't exactly understand what you mean by "linking to #post-[ID]", but going with the question title:
If you use get_permalink() when echoing the link, you will get the permalink - that's just what that function does.
Use get_the_ID() instead, if you want the post-ID returned, or the_ID() if you want it displayed (the_ID() is the same as echo get_the_ID()).
Edited from here:
If you're otherwise happy with the above code, changing
echo '' . $menuPost->post_title . '';
to
echo '' . $menuPost->post_title . '';
ought to do it.
However, I'd go about it like so:
echo '<ul>';
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<li class="title"><h2>' . $category->name . '</h2><ul>';
// query posts of that category:
$query_args = array(
'cat' => $category->cat_ID,
'posts_per_page' => -1
);
$your_query = new WP_Query( $query_args );
// loop through them:
while ( $your_query->have_posts() ) : $your_query->the_post();
echo '<li><a href="#post-';
the_ID();
echo '">';
the_title();
echo '</a></li>';
endwhile;
echo '</ul></li>';
// Reset Post Data
wp_reset_postdata();
}
echo '</ul>';