I use multiple CPT / Taxonomies / Terms in my website.
Currently, to customize, and use the good WP template, i've made a taxonomy-$taxonomy.php
file for each CPT.
But in fact, it's ONLY the CPT and Taxonomy words who are different in my templates.
is exist a solution to make it dynamic ?
UPDATED QUESTION
By saying dynamic, I mean :
Is possible to use only the taxonomy.php
template instead of taxonomy-$taxonomy.php
template and have the 'post_type => '$post_type'
and 'taxonomy' => '$taxonomy'
dynamic
-> What am i trying to achieve is to reduce the templates collection tu use only one because the difference between all my taxonomy-$taxonomy.php
is only post_type and taxonomy.
Bellow, the start of my loop :
<?php
$args = array(
'post_type' => 'cocktails', // Make it dynamic ?
'posts_per_page' => 1,
'order' => 'DESC',
'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'catcocktails', // Make it dynamic ?
'field' => 'slug',
'terms' => get_queried_object()->slug,
)
),
);
// Custom query.
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();
?>
I use multiple CPT / Taxonomies / Terms in my website.
Currently, to customize, and use the good WP template, i've made a taxonomy-$taxonomy.php
file for each CPT.
But in fact, it's ONLY the CPT and Taxonomy words who are different in my templates.
is exist a solution to make it dynamic ?
UPDATED QUESTION
By saying dynamic, I mean :
Is possible to use only the taxonomy.php
template instead of taxonomy-$taxonomy.php
template and have the 'post_type => '$post_type'
and 'taxonomy' => '$taxonomy'
dynamic
-> What am i trying to achieve is to reduce the templates collection tu use only one because the difference between all my taxonomy-$taxonomy.php
is only post_type and taxonomy.
Bellow, the start of my loop :
<?php
$args = array(
'post_type' => 'cocktails', // Make it dynamic ?
'posts_per_page' => 1,
'order' => 'DESC',
'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'catcocktails', // Make it dynamic ?
'field' => 'slug',
'terms' => get_queried_object()->slug,
)
),
);
// Custom query.
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();
?>
Share
Improve this question
edited Apr 3, 2019 at 9:11
WDCreativ
asked Apr 3, 2019 at 8:15
WDCreativWDCreativ
32 silver badges6 bronze badges
2
|
1 Answer
Reset to default 1If your custom taxonomies are unique to the custom post type, then you can set post type to an array ie post_type => array('cpt1', 'cpt2'),
knowing that the taxonomy filter will only return the relevant post types.
Secondly, in your tax_query, change catcocktails
to get_queried_object()->taxonomy
which will make it "dynamic".
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745631846a4637157.html
dynamic
in the sense of querying multiple content types? Or dynamic as in "get all content types and then loop through them"? What exactly do you want to achieve? Could you please update your question for clarification? – norman.lol Commented Apr 3, 2019 at 9:02