0

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(); 

?>
  • 1
    Hey WDCreativ, I honestly don't understand what you are trying to achieve. Do you mean 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? – leymannx Apr 3 at 9:02
  • Hello @leymannx, thanks for your answer. I've updated my question :) – WDCreativ Apr 3 at 9:12
1

If 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".

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.