0

I need a way to have 2 different layouts for Woocommerce prodcut categories. One for categories that contain products and another for categories that show sub-categories. I've been scouring the internet trying to find the relevant code but can't seem to find anything close to what I need.

Does anyone have any ideas as to how I can achieve this on my site?

Thanks in advance,

James

0

One simple way to use different layout for child and parent terms is to check, if the current queried object has parent or not and then include the right template part based on a if statement. For example like this,

// in your taxonomy/term template file
$current_term = get_queried_object();
// parent property is either 0 or the parent ID
if ( $current_term->parent ) {
  get_template_part( 'term-child-template' );
} else {
  get_template_part( 'term-parent-template' );
}

I haven't tested the following example, but you could probably use template_include filter also to switch templates, if the current object is a child or parent term.

function child_or_parent_taxonomy_template( $template ) {
  $current_object = get_queried_object();
  if ( is_a( $current_object, 'WP_Term' ) ) {
    $term_template = $current_tax->parent ? 'child' : 'parent';
    $term_template = locate_template( array( "term-{$term_template}-template.php" ) ); // update as per your setup
    if ( '' !== $term_template ) {
      return $term_template;
    }
  }
  return $template;
}
add_filter( 'template_include', 'child_or_parent_taxonomy_template', 99 );
| improve this answer | |
  • That's great thanks, works a treat! – jamesmonk83 Feb 29 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.