menu

arrow_back How do I output the name of the child categories, and its entries in WP?

by
1 vote
Friends, the question arose

There is a heading (Goods) which includes a subsidiary headings

Category of goods 1
Product category 2

We need to set the page format

Product category 1 product Category 2
Product 1 Product 1-1
Item 2 Item 2-1
Item 3 Item 3-1

I want to use the standard section entries and work with the subcategories of the rubric, that is, they are dynamic, the columns will be added.

or simpler would be to create a separate taxonomy and have to work with the headings and records, without children categories?

1 Answer

by
 
Best answer
0 votes
Can someone come in handy

<?php
$parent_id = 5;
echo '<h2>Услуги</h2>';

# получаем дочерние рубрики
$sub_cats = get_categories( array(
'child_of' => $parent_id,
'hide_empty' => 0
) );
if( $sub_cats ){
foreach( $sub_cats as $cat ){

// Данные в объекте $cat

// $cat->term_id
// $cat->name (Рубрика 1)
// $cat->slug (rubrika-1)
// $cat->term_group (0)
// $cat->term_taxonomy_id (4)
// $cat->taxonomy (category)
// $cat->description ()
// $cat->parent (0)
// $cat->count (14)
// $cat->object_id (2743)
// $cat->cat_ID (4)
// $cat->category_count (14)
// $cat->category_description ()
// $cat->cat_name (Рубрика 1)
// $cat->category_nicename (rubrika-1)
// $cat->category_parent (0)

echo '<h3>'. $cat->name .'</h3>';

# получаем записи из рубрики
$myposts = get_posts( array(
'numberposts' => -1,
'category' => $cat->cat_ID,
'orderby' => 'post_date',
'order' => 'DESC',
) );
# выводим записи
global $post;
foreach($myposts as $post){
setup_postdata($post);
echo '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
}
}

wp_reset_postdata(); // сбрасываем глобальную переменную пост
}