D
D
Denis2021-09-15 09:32:17
WordPress
Denis, 2021-09-15 09:32:17

How to display headings with articles?

Good day colleagues, we set such a task, a separate page, and on it there are headings in the form of an accordion in which there are articles on these headings and this should work automatically, I created a heading and it appeared in the accordion and, accordingly, I created the article marked the heading and it appeared in this heading .

I can just display the headings, I can just display the articles, but I can’t figure out how the tree of headings with entries can be, can anyone come across, or know to suggest in which direction to move, or maybe there is a solution ready or read where you can? I will be very grateful.

6141936af084c562522544.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Zolin, 2021-09-16
@Fetfurmoz

$args = array(
  'taxonomy' => 'category',
  'hide_empty' => true, // не пустые
  'exclude' => [1], // исключаем Без рубрики
);

$terms = get_terms( $args );

if ( $terms ) {
  foreach( $terms as $term ) {

    $args =  array(
      'posts_per_page' => 3, // по три поста
      'post_type' => 'post', // тип записи "посты"
      'post_status' => 'publish', // опубликованные посты
      'tax_query' => array(
        array(
          'taxonomy' => 'category',
          'field'    => 'id',
          'terms'    => $term->term_id
        )
      )
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {

      echo '<h2>' . $term->name . '</h2>';

      while ( $query->have_posts() ) {
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>';
      }

    } else {
      // Постов не найдено
    }

  }
}

T
tyoma_koder, 2021-09-15
@tyoma_koder

display categories
https://wp-kama.ru/function/get_categories
for each category you get posts by specifying the category id
https://wp-kama.ru/function/get_posts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question