G
G
gerales2021-07-12 20:42:39
WordPress
gerales, 2021-07-12 20:42:39

How to display posts in wordpress?

Dear help to understand the code. can't find the error. foreach doesn't work:

<?php
                $terms = get_terms( 'category', [
                  'hide_empty' => true,
                ] );	
            
                foreach ($terms as $term) {
                  //name
                  //term_id
                  //description
                                
              ?>
              <div class="row justify-content-between align-items-center">
                <div class="col-auto">
                  <div class="main__title main__title_min">
                    
                    <?php echo $terms->name ?>
                  </div>
                </div>
                <div class="col-auto">
                  <a href="/category/<?php echo $term->slug; ?>" class="bold link_other"><span>Смотреть все статьи раздела </span> <i class="ico ico__more"></i></a>
                </div>
              </div>
              
              <div class="main__desc">
                <small><?php echo $term->description; ?></small>
              </div>
              <div class="main__slider main__slider_article">
              <?php
                $related = get_posts(
                    array(
                      'numberposts' => 6,
                      'post_type'   => 'post',
                      'tax_query' => array(
                          array(
                            'taxonomy' => 'category',
                            'field' => 'term_id',
                            'terms' => $term->term_id // Where term_id of Term 1 is "1".
                          )
                        )
                    )
                  );
                foreach ($related as $item) {
                    $link = get_permalink($item);
              ?>
                <div class="main__slide">
                  <div class="card card_<?php echo get_post_format($item); ?>">
                    <div class="card__meta">
                      <div class="row">
                        <div class="col-auto">Читайте это</div>
                        <div class="col-auto"><a href="/category/<?php echo $term->slug; ?>"><?php echo $term->name; ?></a></div>
                      </div>
                    </div>
                    <a href="<?php echo $link; ?>" class="card__thumb">
                      <img src="<?php echo get_the_post_thumbnail_url($item, 'thumbnail'); ?>" alt="">
                    </a>
                    <div class="card__title">
                      <a href="<?php echo $link; ?>"><?php echo $item->post_title?></a>
                    </div>
                    <div class="card__data">
                      <?php echo get_the_time('d F Y, h:m', $item); ?>
                      <?php
                        if (get_field('timing', $post)) {
                          echo ' / Время чтения: '.get_field('timing', $item).'.';
                        }
                      ?>
                    </div>
                    <div class="card__more">
                      <a href="<?php echo $link; ?>"><span>Перейти к статье </span> <i class="ico ico__more"></i></a>
                    </div>
                  </div>
                </div>
              <?php
                }
              ?>
              </div>
              <?php
                }
              ?>
              
            </div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladik Bubin, 2021-07-12
@ikoit

https://wp-kama.ru/function/wp_query
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