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)
);
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.
I have this post type that I want to loop through. I have to create two different sections. The top works where it loops through all. But I want to exclude the parent that's holding the children (#6474) and everything in that parent to loop in a different row.
What I have so far. This works in regards of posting all posts. But at the moment includes all parent and children minus #6474. Just want this to show ones that are only parents.
Trying to figure out how to approach creating another row that will only show the children in the post type.
$customersPage_args = array (
'post_type' => array( $global_cat ),
'post_status' => array( 'publish' ),
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'publish_date',
'post__not_in' => array(6474) //excluding the ID holding the children
);
$global_cat_query = new WP_Query( $customers_sort ); ?>
<h3 class="h2 display <?php echo $block[className]; ?>"><?php echo $block_heading; ?></h3>
<div class="card-row">
<div class="card u-pb-0">
<div class="row">
<?php // The Loop
if ( $global_cat_query->have_posts() ) :
while ( $global_cat_query->have_posts() ) : $global_cat_query->the_post(); ?>
<div class="col-md-3 col-sm-4 col-6">
<a href="<?php echo get_permalink(); ?>">
<div class="card card u-mt-0 u-mb-4 align-items-center">
<img src="<?php echo get_the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" />
</div>
</a>
</div>
<?php endwhile;
endif;
// Restore original Post Data
wp_reset_postdata(); ?>
</div>
</div>
</div>
<?php endif; ?>
If you only want top level items, you can make use of the post_parent parameter. If you set it to 0, it will only find "parent" (aka "Top-Level" posts):
$customersPage_args = array (
'post_type' => array( $global_cat ),
'post_status' => array( 'publish' ),
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'publish_date',
'post__not_in' => array(6474), //excluding the ID holding the children
'post_parent' => 0, // Only get "Top Level" posts
);
You can use this function to detect if a post has a parent:
https://developer.wordpress.org/reference/functions/wp_get_post_parent_id/
I have this bit of code:
<div class="wrap sc-cat-container">
<?php
$customPostTaxonomies = get_object_taxonomies('short_courses');
if(count($customPostTaxonomies) > 0)
{
foreach($customPostTaxonomies as $tax)
{
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'taxonomy' => $tax,
'title_li' => '',
'hide_empty' => FALSE
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '
<div class="one-half sc-cat-items">
<h2>
' . $category->name . '
</h2>
</div>';
}
}
}
?>
</div>
This displays a list of categories for my custom post type 'Short Courses' Here's how it looks currently: http://staging.seedcreativeacademy.co.uk/short-courses/
This is great and works fine but I would also like to add images for my categories, I it to look like this:
I have Advanced Custom Fields, where I have created an image field so I can now assign an image to each category. So far so good!
The main problem comes when I try and display the image inside the code above...
Here's the code that would display the image in any normal circumstance:
<img src="<?php the_field('course_type_image'); ?>">
But I guess I need to add it to the above code... so I came up with this but it isn't working as I don't t think I can put a php tag inside another PHP tag!?
<div class="wrap sc-cat-container">
<?php
$customPostTaxonomies = get_object_taxonomies('short_courses');
if(count($customPostTaxonomies) > 0)
{
foreach($customPostTaxonomies as $tax)
{
$args = array(
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
'taxonomy' => $tax,
'title_li' => '',
'hide_empty' => FALSE
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '
<div class="one-half sc-cat-items">
<img src="<?php the_field('course_type_image'); ?>">
<h2>
' . $category->name . '
</h2>
</div>';
}
}
}
?>
</div>
So I'm a bit confused...
You are attempting to echo a string, but within your string, you are calling a function that itself echos rather then returns, a string:
echo '
<div class="one-half sc-cat-items">
<img src="<?php the_field('course_type_image'); ?>">
<h2>
<a href="' . get_category_link( $category->term_id )
. '">' . $category->name . '</a>
</h2>
</div>';
Therefore, when trying to call the function the_field() within your string to be echo'd, it is attempting to echo again.
You need to return the value of the field instead, with the get_field() function.
Also, remove the php tags in the img source line, since you are already within an opening <?php tag:
'<img src="' . get_field('course_type_image') . '">'
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
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;}