custom post types - Dynamic CPTTaxonomy

I use multiple CPTTaxonomiesTerms in my website.Currently, to customize, and use the good WP template, i've m

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745631846a4637157.html

相关推荐

  • custom post types - Dynamic CPTTaxonomy

    I use multiple CPTTaxonomiesTerms in my website.Currently, to customize, and use the good WP template, i've m

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信