L
L
ligisayan2018-10-08 16:46:13
Taxonomy
ligisayan, 2018-10-08 16:46:13

How to display different types of posts for the created taxonomy depending on the query?

Hello! I have a wordpress site with a custom theme.
There are 3 arbitrary types of posts - car brands ( bmw, mazda, ford ) and a taxonomy of classification by years created for them: the base years and its children 2010,2011,2012 , etc.
Created a taxonomy-years.php template where I display a list of cars by years

<?php $cat = get_term_by('name', single_cat_title('',false), 'years'); $catSlug = $cat->slug; 
                $mypost = array(
                  'post_type' => 'bmw',
                  'posts_per_page' => -1,
                  'tax_query' => array(
                    array(
                      'taxonomy' => 'years',
                      'field' => 'slug',
                      'terms' => $catSlug
                    ),
                  ),
                );
                $loop = new WP_Query( $mypost );
              ?>
              <?php while ( $loop->have_posts() ) : $loop->the_post();?>
             <div>Характеристики авто</div>
<?php endwhile; wp_reset_postdata(); ?>

Those. from the car brand page I follow the link to some thread of the year and I see a list of cars with characteristics, BUT, in order to get it, I need to clearly indicate the post type ('post_type' => 'bmw') , i.e. using this template, I can only display information on years for only one type.
And how to make it possible to infer for everyone, while maintaining a single taxonomy of years over years for everyone?
Those. so that you can add the desired variable to 'post_type', and not a static constant.
I perfectly understand that you can create your own taxonomy for each post, but still I would like to limit myself to one and one template.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2018-10-08
@ligisayan

1. The post_type parameter in WP_Query can take an array, not just one bmw, string|array there.
2. The same taxonomy can be registered for a set of custom post types, there is also a string|array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question