M
M
Mariana Marianova2018-05-03 14:48:39
WordPress
Mariana Marianova, 2018-05-03 14:48:39

How to display wordpress posts from a specific category in two columns?

There is an example code with the output, as I understand it, of all posts on the page, but I need to display not all entries, but from a certain category (id=5, name - skills). Please tell me how to do it?

<?php if (have_posts()) { ?> 
  <?php $i = 0; ?> 
  <?php $per_column = ceil($posts_per_page / 2); ?> 
  <?php if ($wp_query->post_count <= $posts_per_page) $per_column = ceil($wp_query->post_count / 2); ?> 
 
          <div class="column"> 
 
  <?php while (have_posts()) { the_post(); $i++; ?> 
 
              <div class="post"> 
                  <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2> 
                  <div class="date"><?php the_time('d.m.Y') ?></div> 
                  <div class="entry"> 
                      <?php the_excerpt(); ?> 
                  </div> 
                </div><!-- .post --> 
 
     <?php if ($i == $per_column) { ?> 
          </div><!-- .column --> 
 
         <div class="column right"> 
      <?php } ?> 
 
  <?php } ?> 
 
          </div><!-- .column --> 
 
    <?php } ?>

PS I finally found a solution
<?php $args = array(
        'post_type' => 'skills',
        'post_status' => 'publish'
      );
      $skills_loop = new WP_Query( $args );
      ?>

      <?php if ($skills_loop->have_posts()) { ?> 
      <?php $i = 0; ?> 
      <?php $per_column = ceil($posts_per_page / 2); ?> 
      <?php if ($skills_loop->post_count <= $posts_per_page) $per_column = ceil($skills_loop->post_count / 2); ?> 

      <div class="column"> 
        <ol class="skills-list">

          <?php while ($skills_loop->have_posts()) { $skills_loop->the_post(); $i++; ?> 

          <li><span class="dec"><?php echo $i; ?></span>
            <span class="skills-text"><?php the_title(); ?></span></li>

            <?php if ($i == $per_column) { ?> 
          </ol>
        </div><!-- .column --> 

        <div class="column"> 
          <ol class="skills-list">
            <?php } ?> 

            <?php } ?> 
          </ol>
        </div><!-- .column --> 

        <?php } ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2018-05-03
@azerphoenix

You can display articles in 2 columns in different ways.
1) using the PHP method by setting the counter
2) using the css method, the column-count property
Useful article - wordsmall.ru/bez-plagina/vyvodim-posty-v-dve-kolon...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question