T
T
Tempest992020-04-20 23:19:30
WordPress
Tempest99, 2020-04-20 23:19:30

How to display pages with taxonomy?

I study WordPress by video and until the 15th minute everything worked out for me, namely, before taxonomy.php is created and the author himself also does not know how to make pages with a certain taxonomy appear. Maybe someone here can help figure it out. https://www.youtube.com/watch?v=7GNaMaRQkT0&list=P...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Komchev, 2020-04-21
@Lebezniy

If I understood you correctly, then this code will suit you. pay attention to

'tax_query'

<?php
$mypost_Query = new WP_Query( array(
    'post_type'      => 'post', // тип записи: post, page, custom_post_type
    'post_status'    => 'publish', // статус записи
    'posts_per_page' => -1,        // кол-во записей (-1 все)
    
    'tax_query' => array(        // если элемент таксономии
        array(
            'taxonomy' => '{название_таксономии}',  // таксономия (категория)
            'field'    => 'slug',                   // тип поля slug или id
            'terms'    => '{элемент(ы)_таксономии}' // ярлык или id
        )
    )
) );

if ( $mypost_Query->have_posts() ) {
    while ( $mypost_Query->have_posts() ) { $mypost_Query->the_post();

        get_template_part('./template-parts/loop-myposts'); // шаблон для отображения каждой записи

    } wp_reset_postdata(); // "сброс"

} else { echo '<p>Извините, нет записей ...</p>'; } ?>

Wordpress cheat sheet

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question