B
B
Boris Belov2015-10-13 19:40:37
PHP
Boris Belov, 2015-10-13 19:40:37

How to display multiple categories with Wordpress posts?

Good evening.

Task:
On the main page, I want to display the categories. All who are created and who are.
But they should be limited to only three entries .

Visually something like this:

8c1ae0cf3d724f76a66ce60427ad554b.png

In letters something like this:

Рубрика 1    Рубрика 2     Рубрика 3
Запись 1      Запись 1       Запись 1
Запись 2      Запись 2       Запись 2
Запись 3      Запись 3       Запись 3


Now I have entries displayed through (code below), and only entries are displayed, without the heading of the general category in which they are located.

<section>
  <?php $posts = get_posts ("category=12&orderby=date&order=ASC&numberposts=3"); ?> 
<?php if ($posts) : ?>
<?php foreach ($posts as $post) : setup_postdata ($post); ?>
 
  <div class="block">
     <div class="name"> 
          <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a> 
     </div>
  </div>
 
<?php endforeach; ?>
<?php endif; ?>
</section>


What solution can you suggest?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Martirosov, 2015-10-13
@kalbac

Something like that

<?php
  $categories = get_categories();
  
  foreach( $categories as $category ) {
    
    echo $category->name;
    
    $posts = get_posts( array(
      'post_type'		=> 'post',
      'posts_per_page'=> 3,
      'category'		=> $category->term_id
    ) );
    
    foreach( $posts as $post ) {
      echo $post->post_title;
    }
  }
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question