最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Dynamic CPTTaxonomy

programmeradmin2浏览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(); 

?>

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

发布评论

评论列表(0)

  1. 暂无评论