How to display post category wise in wordpress custom theme

Make sure to replace the category name in the third line with your desired category. This snippet will create the shortcode [Last5Posts], which will display the most recent five posts from the selected category.

woocommerce_product_category

<?php
$args = array(
'taxonomy' => 'product_cat', // Product category taxonomy
'orderby' => 'name', // Order by name
'hide_empty' => false, // Show categories with no products
);

$product_categories = get_terms( $args );

if ( !empty($product_categories) ) {
echo '<ul>';
foreach ( $product_categories as $category ) {
echo '<li><a href="' . get_term_link( $category ) . '">' . $category->name . '</a></li>';
}
echo '</ul>';
}
?>
woocommerce_product_category ( display only parent category )

<?php
$args = array(
'taxonomy' => 'product_cat',
'parent' => 0, // Only parent categories
'hide_empty' => false,
);

$parent_categories = get_terms( $args );

foreach ( $parent_categories as $parent_category ) {
echo '<h2>' . $parent_category->name . '</h2>';

// Display child categories
$child_args = array(
'taxonomy' => 'product_cat',
'parent' => $parent_category->term_id, // Get children of this parent
'hide_empty' => false,
);

$child_categories = get_terms( $child_args );

if ( !empty($child_categories) ) {
echo '<ul>';
foreach ( $child_categories as $child_category ) {
echo '<li><a href="' . get_term_link($child_category) . '">' . $child_category->name . '</a></li>';
}
echo '</ul>';
}
}
?>
woocommerce_product_category ( display child category on the basis of parent categories )

<?php
$args = array(
'taxonomy' => 'product_cat',
'parent' => 0, // Only parent categories
'hide_empty' => false,
);

$parent_categories = get_terms( $args );

foreach ( $parent_categories as $parent_category ) {
echo '<h2>' . $parent_category->name . '</h2>';

// Display child categories
$child_args = array(
'taxonomy' => 'product_cat',
'parent' => $parent_category->term_id, // Get children of this parent
'hide_empty' => false,
);

$child_categories = get_terms( $child_args );

if ( !empty($child_categories) ) {
echo '<ul>';
foreach ( $child_categories as $child_category ) {
echo '<li><a href="' . get_term_link($child_category) . '">' . $child_category->name . '</a></li>';
}
echo '</ul>';
}
}
?>


// category and sub category thumbnails:

<?php
$term_id = get_queried_object()->term_id; // Get current category ID
$thumbnail_id = get_term_meta($term_id, 'thumbnail_id', true); // Get category thumbnail ID
$image_url = wp_get_attachment_url($thumbnail_id); // Get the image URL
?>

woocommerce_product_category ( product_category.php )

<?php
// Get the current category object
$current_category = get_queried_object();

// If we're on a category page and it's a product category
if ( is_product_category() && isset( $current_category->term_id ) ) {
// Get the parent category ID
$parent_category_id = $current_category->term_id;

// Get the subcategories (child categories) for the parent category
$args = array(
'taxonomy' => 'product_cat', // WooCommerce product categories
'parent' => $parent_category_id,
'hide_empty' => false, // Optionally hide empty categories
);
$subcategories = get_terms( $args );

// If there are subcategories, loop through them
if ( ! empty( $subcategories ) && ! is_wp_error( $subcategories ) ) {
echo '<ul class="subcategories-list">';
foreach ( $subcategories as $subcategory ) {
// Display each subcategory
$subcategory_link = get_term_link( $subcategory );
echo '<li class="subcategory-item">';
echo '<a href="' . esc_url( $subcategory_link ) . '">' . esc_html( $subcategory->name ) . '</a>';

// Optionally, show subcategory thumbnail
if ( function_exists( 'z_taxonomy_image_url' ) ) {
$thumbnail_url = z_taxonomy_image_url( $subcategory->term_id );
if ( $thumbnail_url ) {
echo '<img src="' . esc_url( $thumbnail_url ) . '" alt="' . esc_attr( $subcategory->name ) . '" />';
}
}
echo '</li>';
}
echo '</ul>';
} else {
echo 'No subcategories found.';
}
}
?>


index.php / page.php ( blog category )

<?php 
$cat = get_terms([
    'taxonomy' => 'category', 
    'hide_empty' => false // return that category where no post add
]);

foreach ($cat as $key => $category) {
   echo '<a href="'.get_category_link($category->term_id).'">'.$category->name.'</a><br/>';

$args = [
   'post_type' => 'galleries',
   'post_status' => 'publish',
   'tax_query' => [
       'taxonomy' => $category->name,
       'field' => 'term_id',
       'terms' => $category->term_id,
]];

// The Query.

$the_query = new WP_Query( $args );

  if ( $the_query->have_posts() ) : while( $the_query->have_posts()  ) : $the_query->the_post(); ?>

    <h1>
      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </h1>

    <a href="<?php the_permalink(); ?>">
        <?php the_post_thumbnail( 'thumbnail' ); ?>
    </a>
    <p><?php the_excerpt(); ?></p>

<?php endwhile; endif; 

}

?>
Product category with thumbnail

<?php
$args = array(
'taxonomy' => 'product_cat', // WooCommerce product category taxonomy
'orderby' => 'name', // Order categories by name
'hide_empty' => false, // Show even categories with no products
);

$product_categories = get_terms( $args );

if ( ! empty( $product_categories ) && ! is_wp_error( $product_categories ) ) {
echo '<ul class="product-categories">';

foreach ( $product_categories as $category ) {
// Get category thumbnail
$thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true );
$thumbnail_url = wp_get_attachment_url( $thumbnail_id );

// Display the category and thumbnail
echo '<li>';
echo '<a href="' . esc_url( get_term_link( $category ) ) . '">';

// Display category thumbnail
if ( $thumbnail_url ) {
echo '<img src="' . esc_url( $thumbnail_url ) . '" alt="' . esc_attr( $category->name ) . '" class="category-thumbnail" />';
}

// Display category name
echo '<h3>' . esc_html( $category->name ) . '</h3>';
echo '</a>';
echo '</li>';
}

echo '</ul>';
}
?>
Product category with product data and thumbnail

<?php
$product = wc_get_product( get_the_ID() ); // Get the current product
$categories = get_the_terms( $product->get_id(), 'product_cat' ); // Get the product's categories

if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
echo '<ul class="product-categories">';

foreach ( $categories as $category ) {
// Get category thumbnail
$thumbnail_id = get_term_meta( $category->term_id, 'thumbnail_id', true );
$thumbnail_url = wp_get_attachment_url( $thumbnail_id );

// Display category and thumbnail
echo '<li>';
echo '<a href="' . esc_url( get_term_link( $category ) ) . '">';

if ( $thumbnail_url ) {
echo '<img src="' . esc_url( $thumbnail_url ) . '" alt="' . esc_attr( $category->name ) . '" class="category-thumbnail" />';
}

echo '<h3>' . esc_html( $category->name ) . '</h3>';
echo '</a>';
echo '</li>';
}

echo '</ul>';
}
?>

Leave a Reply