So in the starter theme we have this:
archive.php:
$templates = array( 'archive.twig', 'index.twig' );
$context = Timber::get_context();
$context['title'] = 'Archive';
if ( is_day() ) {
$context['title'] = 'Archive: ' . get_the_date( 'D M Y' );
} else if ( is_month() ) {
$context['title'] = 'Archive: ' . get_the_date( 'M Y' );
} else if ( is_year() ) {
$context['title'] = 'Archive: ' . get_the_date( 'Y' );
} else if ( is_tag() ) {
$context['title'] = single_tag_title( '', false );
} else if ( is_category() ) {
$context['title'] = single_cat_title( '', false );
array_unshift( $templates, 'archive-' . get_query_var( 'cat' ) . '.twig' );
} else if ( is_post_type_archive() ) {
$context['title'] = post_type_archive_title( '', false );
array_unshift( $templates, 'archive-' . get_post_type() . '.twig' );
}
$context['posts'] = new Timber\PostQuery();
Timber::render( $templates, $context );
by my understanding, if I navigate to http://.......com/index.php/category/newcategory/ it should go grab the archive-newcategory.twig file, as a template. Another example, if i go to http://.......com/index.php/category/anothercat/ it should go grab the archive-anothercat.twig . Is it possible that i am understanding something wrong? Because if this is the case, my starter theme does not work as intended. I can't find a dynamic solution in the docs, if this isn't the one.
It’s working as intended. When is_category()
is true, the archive will fetch the category ID through get_query_var( 'cat' )
, and not the category name.
You can update the code in archive.php to add the Twig template you’ll want to use. For example:
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' );
}
Or you could also use a different PHP template. Consider the list of PHP templates on wphierarchy.com. There you can see that you could use a category.php file in your theme root:
$context = Timber::get_context();
$context['title'] = single_cat_title( '', false );
Timber::render( 'archive-category.twig', $context );