I am working on a Timber/Twig template and have created a Custom Post Type with a Taxonomy specific to that Post Type.
$tour_labels = array(
'name' => _x( 'Tours', 'Post Type General Name', 'standrews' ),
'singular_name' => _x( 'Tour', 'Post Type Singular Name', 'standrews' ),
'menu_name' => __( 'Tours', 'standrews' )
);
$tour_rewrite = array(
'slug' => 'tours-excursion',
'with_front' => false,
'pages' => true,
'feeds' => true,
);
$tour_args = array(
'label' => __( 'Tours', 'standrews' ),
'description' => __( 'Tour Description', 'standrews' ),
'labels' => $tour_labels,
'supports' => array( 'title', 'excerpt', 'thumbnail', 'trackbacks', 'revisions', ),
'taxonomies' => array( 'post_tag', 'tour_category' ),
'hierarchical' => false,
'has_archive' => 'tours-excursions',
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $tour_rewrite,
'capability_type' => 'post',
);
register_post_type( 'tours', $tour_args );
Taxomony to create Tour Categories
$tours_labels = array(
'name' => _x( 'Categories Tours', 'Taxonomy General Name', 'standrews' ),
'singular_name' => _x( 'Category Tour', 'Taxonomy Singular Name', 'standrews' ),
'menu_name' => __( 'Categories Tours', 'standrews' )
);
$tours_rewrite = array(
'slug' => 'tours-excursions',
'with_front' => true,
);
$tours_args = array(
'labels' => $tours_labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $tours_rewrite,
);
register_taxonomy( 'tour_category', array( 'tours' ), $tours_args );
Inside the tour_category Taxonomy, I have 3 Categories (Sightseeing Tours, Golf Breaks & Walking Tours)
I have created an archive-tours.php file to call the archive-tours.twig
$templates = array('archives/archive-tours.twig');
$context = Timber::get_context();
$context['title'] = 'Archive Tours';
if (is_category()){
$context['title'] = single_cat_title('', false);
array_unshift($templates, 'archives/archive-'.get_query_var('cat').'.twig');
} else if (is_post_type_archive()){
$context['title'] = post_type_archive_title('', false);
array_unshift($templates, 'archives/archive-'.get_post_type().'.twig');
}
$context['posts'] = Timber::get_posts();
Timber::render($templates, $context);
The Tours Twig template
{% block content %}
<p>Archive (archive-tours.twig)</p>
{% for post in posts %}
{% include ['teases/_tease-'~post.post_type~'.twig', 'teases/tease.twig'] %}
{% endfor %}
{% endblock %}
Ideally what I am aiming for is a way to call a separate tease template for the 3 categories, however unsure about how to achieve this.
I am working on a Timber/Twig template and have created a Custom Post Type with a Taxonomy specific to that Post Type.
$tour_labels = array(
'name' => _x( 'Tours', 'Post Type General Name', 'standrews' ),
'singular_name' => _x( 'Tour', 'Post Type Singular Name', 'standrews' ),
'menu_name' => __( 'Tours', 'standrews' )
);
$tour_rewrite = array(
'slug' => 'tours-excursion',
'with_front' => false,
'pages' => true,
'feeds' => true,
);
$tour_args = array(
'label' => __( 'Tours', 'standrews' ),
'description' => __( 'Tour Description', 'standrews' ),
'labels' => $tour_labels,
'supports' => array( 'title', 'excerpt', 'thumbnail', 'trackbacks', 'revisions', ),
'taxonomies' => array( 'post_tag', 'tour_category' ),
'hierarchical' => false,
'has_archive' => 'tours-excursions',
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $tour_rewrite,
'capability_type' => 'post',
);
register_post_type( 'tours', $tour_args );
Taxomony to create Tour Categories
$tours_labels = array(
'name' => _x( 'Categories Tours', 'Taxonomy General Name', 'standrews' ),
'singular_name' => _x( 'Category Tour', 'Taxonomy Singular Name', 'standrews' ),
'menu_name' => __( 'Categories Tours', 'standrews' )
);
$tours_rewrite = array(
'slug' => 'tours-excursions',
'with_front' => true,
);
$tours_args = array(
'labels' => $tours_labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $tours_rewrite,
);
register_taxonomy( 'tour_category', array( 'tours' ), $tours_args );
Inside the tour_category Taxonomy, I have 3 Categories (Sightseeing Tours, Golf Breaks & Walking Tours)
I have created an archive-tours.php file to call the archive-tours.twig
$templates = array('archives/archive-tours.twig');
$context = Timber::get_context();
$context['title'] = 'Archive Tours';
if (is_category()){
$context['title'] = single_cat_title('', false);
array_unshift($templates, 'archives/archive-'.get_query_var('cat').'.twig');
} else if (is_post_type_archive()){
$context['title'] = post_type_archive_title('', false);
array_unshift($templates, 'archives/archive-'.get_post_type().'.twig');
}
$context['posts'] = Timber::get_posts();
Timber::render($templates, $context);
The Tours Twig template
{% block content %}
<p>Archive (archive-tours.twig)</p>
{% for post in posts %}
{% include ['teases/_tease-'~post.post_type~'.twig', 'teases/tease.twig'] %}
{% endfor %}
{% endblock %}
Ideally what I am aiming for is a way to call a separate tease template for the 3 categories, however unsure about how to achieve this.
Share Improve this question asked Jan 26, 2017 at 19:21 RichardRichard 111 silver badge4 bronze badges1 Answer
Reset to default 3It's a late reply and I suspect you might have it figured out already, but if someone is looking for the same info, here's an example.
{% for post in posts %}
{% set post_terms = post.terms('tour_category') %}
{% if function( 'has_term', 'Sightseeing Tours', 'tour_category' ) %}
{% set teaser = 'sightseeing' %}
{% elseif function( 'has_term', 'Golf Breaks', 'tour_category' ) %}
{% set teaser = 'golfbreak' %}
{% elseif function( 'has_term', 'Walking Tours', 'tour_category' ) %}
{% set teaser = 'wlkingtour' %}
{% endif %}
{% include ['teases/_tease-'~teaser~'.twig', 'teases/tease.twig'] %}
{% endfor %}