Answer the question
In order to leave comments, you need to log in
How to display wordpress posts like this?
Hello.
I have a custom post type "Products".
there are many categories (vegetables, fruits, etc.)
I want to display in this way on the page:
category vegetables
- entry cucumber
- entry potato
category fruit
- entry apple
- entry pear
alcohol category
- entry beer
- entry wine
I managed to make a list of categories and a list of articles, but not together.
Thanks
Answer the question
In order to leave comments, you need to log in
I asked myself, I answered
<?php
$arguments = array(
'orderby' => 'name',
'order' => 'ASC',
'number' => 0,
'taxonomy' => 'calc_cat',
'field' => 'slug',
);
$cats = get_categories($arguments);
foreach ($cats as $cat) {
echo $cat->cat_name; // Получаем название одной категории
$psts = query_posts( array(
'posts_per_page' => -1,
'post_type' => 'calc',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'calc_cat',
'field' => 'slug',
'terms' => $cat->cat_name
))
) );
foreach( $psts as $pst ){
echo $pst->post_title;//вывод продукта
}
} wp_reset_query();
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question