I have a parent category page in WordPress that displays a single post and four sub categories below it. The four sub category pages will share the same HTML and logic, but are different from the parent category. The parent category is using category.php and category.twig to handle the display of its HTML and content. See code below. How can I tell WordPress and Timber to use category-child.php and category-child.twig (example names) for children of the parent category (slug: stories). I know I can use category-slug.php or category-id.php for each child slug or ID, but that would require adding the same code to four (or more) different PHP and Twig files, which is not ideal.
category.php
/**
* @package WordPress
* @subpackage gerryfeehan
* @since 1.0.0
*/
$templates = array( 'category.twig', 'index.twig' );
$context = Timber::context();
$args = array(
'cat' => '7,5,3,4,6',
'numberposts' => 1,
'orderby' => 'date',
'order' => 'DESC',
);
$context['stories'] = Timber::get_posts($args);
$context['categories'] = Timber::get_terms('category');
$categories = get_categories(
array(
'hide_empty' => '0',
'parent' => 7,
'orderby' => 'id',
'order' => 'ASC'
)
);
// $context['categories'] = $categories;
// Updated code with suggestions from Tomek
$category = get_queried_object(); // will give you current WP_Term
if( $category->parent == 0 ) {
// parent category
$templates = array( 'category.twig' );
$context['categories'] = $categories;
} else {
// child category
$templates = array( 'category-map.twig' );
$context['categories'] = $categories;
}
// Timber::render( array( 'category.twig', 'index.twig' ), $context );
Timber::render( $templates, $context );
category.twig
{% extends "base.twig" %}
{% block content %}
<section class="stories">
<h2>Stories</h2>
{% for story in stories %}
<article class="story" id="story-{{ story.ID }}">
{% if story.thumbnail.src %}
<figure>
<img src="{{ story.thumbnail.src }}" class="" alt="{{ story.thumbnail.alt }}" />
{% if story.thumbnail.caption %}
<figcaption>{{ story.thumbnail.caption }}</figcaption>
{% endif %}
</figure>
{% endif %}
<h3 class="story__heading">
<a href="{{ story.link }}">
{{ story.title }}
</a>
</h3>
<div class="story__meta">
<time class="">{{ story.date }}</time>
</div>
<div class="story__content">
{{ story.preview.read_more(false) }}
</div>
</article>
{% endfor %}
</section>
{% if function(cat_is_ancestor_of(7, 5)) %}yolo{% endif %}
{% for category in categories %}
<div class="category">
<figure>
<figcaption>
{{ category.name }}
</figcaption>
{{ category.description }}
</figure>
</div>
{% endfor %}
{% endblock %}
The following code could be added to the archive.php file, but I am not sure how it could be expanded to meet my needs.
else if ( is_category() ) {
$term = new Timber\Term( get_queried_object_id() );
$context['term'] = $term;
$context['title'] = single_cat_title( '', false );
array_unshift( $templates, 'archive-' . $term->slug . '.twig' );
}
Full parent and child category structure
Parent: Stories Children: Camping America, Canada, United States, and World.
Any ideas?