I have been working on a plugin based on woocommerce. As a part of this I am trying to list all the category links in a page using a shortcode and I have succeeded in this.
The issue is all the category links with zero products are returning 404 error page where as the link works fine if the category has one or more products. Did anyone face this issue earlier?
Updating with Code:
$args = array('taxonomy' => 'product_cat', 'hide_empty' => false, 'parent' => 0, 'exclude' => 723);
$terms = get_terms('product_cat', $args);
if (!$category instanceof WP_Error) {
foreach ($terms as $term):
?>
<ul class="col-md-3 col-lg-3 col-sm-6 col-xs-6 directory_catblock">
<h3>
<?php echo $term->name; ?>
</h3>
<?php
$args_sub = array('taxonomy' => 'product_cat', 'hide_empty' => false, 'parent' => $term->term_id);
$terms_sub = get_terms('product_cat', $args_sub);
foreach ($terms_sub as $term_sub):
?>
<li>
<?php echo $term_sub->name; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach;}
Related
I have created a foreach loop for the category section in my sidebar. Everything is working good, however, I don't want to allow one of the categories to show. I have not been able to find anything on how to null out an object in query or anything.
here is my code, the name of the category is "general information"
<aside class="sidebar">
<div class="category" data-aos="fade-left" data-aos-delay="300">
<h2>Popular Categories</h2>
<ul class="category-list">
<?php
$args = array(
'orderby' => 'count',
'order' => 'DESC'
);
$terms = get_terms('category', $args); /*Name Of category*/
foreach (array_slice($terms, 0, 5) as $term ):
?>
<li class="list-items" data-aos="fade-left" data-aos-delay="400">
<a href="<?php echo site_url('/general');?>">
<?php echo
$term->name ;?> </a>
<span>(<?php echo $term->count ;?>)</span>
</li>
<?php
endforeach;
wp_reset_query();
?>
</ul>
</div>
I am guessing that there is a call I can put in the $args(). Any help would be greatly appreciated.
$args = array(
'orderby' => 'count',
'order' => 'DESC'
'exclude' => array( 77 ), // ID of Category which you don't want to show
);
You can exclude like this.
The snippet below works fine but I've hard coded the parent categories IDs, Now I'm looking for a way to get rid of hard coded IDs.
<div class="row">
<?php
$catsArray = array(176, 175); // This line need to be dynamic and the IDs are parent categories.
$categories = get_terms(
array(
'hide_empty' => false,
'include' => $catsArray, 'orderby' => 'include'
) );
foreach ($categories as $key => $cat) {
$cat_thumb_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
$term_link = get_category_link( $cat->term_id );
?>
<div class="col-md-6">
<div class="sellers-wrap is-revealing">
<figure>
<img src="<?php echo $cat_img; ?>" alt="" class="img-fluid">
</figure>
<div class="sellers-text">
<p><strong><?php echo $cat->name; ?></strong></p>
</div>
</div>
</div>
<?php } ?>
</div>
The first parameter you have to get_terms is an array of arguments. All possible values are documented here: https://developer.wordpress.org/reference/classes/wp_term_query/__construct/ . It sounds like you just want to get all categories that are at the top level of the hierarchy (i.e. have no parent). So to do that you can just use the 'parent' argument and pass 0. Like this:
$categories = get_terms([
'hide_empty' => false,
'parent' => 0
]);
If 0 is passed, only top-level terms are returned.
$categories = get_terms(
'category',
array('parent' => 0)
);
I created a custom post taxonomy.Now i want to show all specific post by specific taxonomy. so I created a taxonomy-product_cat.php page.
here is product page get term and link code--
<div class="side_box side_box_1 red5">
<h5>Filter Products</h5>
<h6>Brand</h6>
<?php $topics = get_terms('product_cat');
echo '<ul class="advanced-filters tgl_c">';
foreach ($topics as $topic) {
echo '<li class="advanced-filter" data-group="Brand">'.$topic->name.'</li>';
}
echo '</ul>';?>
</div>
here is custom post taxonomy code---
function product_taxonomy() {
register_taxonomy(
'product_cat', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'product', //post type name
array(
'hierarchical' => true,
'label' => 'product Category', //Display name
'query_var' => true,
'show_admin_column' => true,
'rewrite' => array(
'slug' => 'product-category', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'product_taxonomy');
And here is taxonomy-product_cat.php page code--
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $unique = "_product_"; ?>
<div class="col-md-3 col-xs-6 element mb30">
<div class="main_box">
<div class="box_1">
<div class="product-image">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( $post->ID, 'product-image',array('class' => 'img-responsive') );?>
</a>
</div>
<div class="overlay hidden-sm hidden-xs">
More Info
</div>
</div>
<div class="desc">
<h5><?php the_title(); ?></h5>
<p><?php echo get_post_meta(get_the_ID(),$unique.'brand_name',true); ?></p>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else :?>
<h3><?php _e( 'Not Found Any Product.' ); ?></h3>
<?php endif ?>
But the result is Not Found Any Product.So please someone help me how can i fix this problem.Thanks
see documentation
https://developer.wordpress.org/reference/functions/get_terms/
specifically
Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:
$terms = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
) );
instead of the first parameter being the taxonomies.
get_terms('product_cat');
You should do
get_terms(array(
'taxonomy' => 'product_cat'
));
Not sure if that is the issue, but seems the most obvious thing.
UPDATE
Did you try this
get_terms(array(
'taxonomy' => 'product_cat',
'hide_empty' => 0
));
This will show the taxonomy if you didn't insert any actual terms with
wp_insert_term()
As per this page,
https://wordpress.org/support/topic/get_terms-does-not-return-my-custom-taxonomy-terms
I don't typically work with WP on this level, but my google skills are unmatched ... lol
EDIT: After changing the tax_query to slug it now outputs posts in the first loop (the first cat) but does not in the next tab/cat which I know has many published posts assigned to it.
I created a shortcode that is meant to grab all the categories in a CPT and out them into bootstrap tabs with a certain amount of posts per tab.
To do this I do get_categories for the tabs nav (Works fine).
Then I do get_categories again to output the tabs content (Also works fine) with a wp_query inside each where I grab the posts. This is what is not working. No output.
If I echo the category name or any data I can grab from the get_categories it echo's it. So it seems the issue is with the WP_Query.
Here is the code:
add_shortcode( 'tabbed_grid_articles', 'tabbed_grid_articles' );
function tabbed_grid_articles($args = array()){
global $post;
$current_id = $post->ID;
$defaults = apply_filters( 'dy_tabbed_grid_default_args', array(
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC',
'echo' => true,
'post_type' => 'playbooks',
'category' => 0,
'hide_empty' => true,
'exclude' => '',
'include' => '',
'taxonomy' => 'playbooks_category',
));
$args = wp_parse_args( $args, $defaults );
$cat_args = array(
'taxonomy' => $args['taxonomy'],
'orderby' => 'menu_order',
'order' => 'ASC',
'hierarchical' => true,
'parent' => $args['category'],
'hide_empty' => $args['hide_empty'],
'child_of' => $args['category'],
'exclude' => $args['exclude'],
'include' => $args['include']
);
$categories = get_categories($cat_args);
?> <ul class="nav nav-tabs" role="tablist"> <?php
$count = 0;
foreach($categories as $category){
$targetID = str_replace(' ', '-', $category->cat_name);
?> <!-- Nav tabs -->
<li role="presentation" class="<?php if($count == 0){echo 'active'; $count ++;} ?>"><?php echo $category->cat_name ?></li>
<?php
}
// wp_reset_postdata();
?> </ul>
<!-- Tab panes -->
<div class="tab-content">
<?php
$count = 0;
foreach($categories as $category) {
$targetID = str_replace(' ', '-', $category->cat_name);
$args['tax_query'] = array(array(
'taxonomy' => $args['taxonomy'],
'field' => 'slug',
'terms' => $category->cat_name,
),
);
// $args['cat'] = $category->cat_ID;
$grid_query = new WP_Query($args);
?>
<div role="tabpanel" class="tab-pane <?php if($count == 0){echo 'active'; $count ++;} ?>" id="tab-<?php echo $targetID ?>">
<?php
if($grid_query->have_posts()) {
while ($grid_query->have_posts()) {
$grid_query->the_post();
echo get_the_title();
//echo '1<br>';
//echo $args['cat'];
//echo $category->cat_name;
//echo get_the_excerpt();
echo '<br>';
//the_excerpt();
}
wp_reset_postdata();
}
?></div>
<?php
}
?>
</div>
<?php
}
I tried $args['cat'] = $category->cat_ID; instead of tax_query but neither work.
If I comment them both out then I get all the posts from the cpt. Currently just echoing the excerpt...
Any ideas?
Solved. I took for granted that the slug was the same as the name and it was not.
I think I only changed the slug to be the name by mistake when debugging at first when it was not working. Anyway, changed 'terms' => $category->cat_name, to 'terms' => $category->slug, and it worked.
Hope this helps someone in the future.
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