最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom post types - Dynamic CPTTaxonomy

matteradmin7PV0评论

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 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? – norman.lol Commented Apr 3, 2019 at 9:02
  • Hello @leymannx, thanks for your answer. I've updated my question :) – WDCreativ Commented Apr 3, 2019 at 9:12
Add a comment  | 

1 Answer 1

Reset to default 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".

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far