How to loop posts in Wordpress?

Asked

Viewed 1,084 times

2

Guys I need to create that provision of articles in my Wordpress loop and do not know how I could do. I know it will require some instructions in PHP and some implementations of Bootstrap, I’m using this framework CSS.

Does anyone have any idea how I can start thinking about this structure?

2 answers

2

Apparently there are some posts that have photo that is 2x1, and others that have photo of 1x2 (I refer to these photos):

1x2

2x1

In addition, there are 3 columns. Bootstrap can do this easily:

<div class="row">
  <div class="col-md-4">
    <!-- conteudo -->
  </div>
  <div class="col-md-4">
    <!-- conteudo -->
  </div>
  <div class="col-md-4">
    <!-- conteudo -->
  </div>
</div>

Your logic, however, will have to analyze the image of each post within the loop and check:

  1. If this post is part of this <div class="row">, or you need to start another
  2. If the post is listed as one or two columns.
  3. If the post appears as two Rows, you will have to skip the third column in the next Row

Example

Obs: This is not code you can just copy and paste into your application - it’s just an attempt to show something similar to direct you on the right path.

<?php 
 $colunas = 3;
 $coluna_corrente = 0;
 $duas_rows = false;
?>
<?php if ( have_posts() ) : ?>
 <?php while( have_posts() ) : the_post(); ?>
   <?php
    ++$coluna_corrente;
    if ( $coluna_corrente % $colunas == 0 ) {
      echo "<div class='row'>";
    }
    if ( tamanho_relativa_da_foto( $colunas, $post->foto ) < 2 ) { //esse função não existe
     echo '<div class="col-md-4">';
     the_title();
     //algo mais
     echo "</div>";
    } else if ( tamanho_relativa_da_foto( $colunas, $post->foto ) == 2 && !$duas_rows ) {
     echo '<div class="col-md-8">';
     //etc...
     echo '</div>';
    } else {
      $duas_rows = true;
      echo '<div class="col-md-4">';
      //...
      echo '</div>';
    }
    if ( $coluna_corrente == $colunas ) {
      echo "</div>";
    }
   ?>
 <?php endwhile; ?>
<?php endif; ?>
  • Very intuitive your answer. As for the images, there will be no problems, What I really want is to respect the positioning of the layout without worrying for now with what goes inside. Can you help me to do the 3 checks in order to get the desired result? Could you show me a part to try to do the rest [PHP] ...

  • I can offer you some psuedocode, but I won’t be able to write down exactly what you need to get what you’re trying to do - unless you want to pay my way to your country, and then I’ll be happy to go! : D

  • rsrsrsrs ... I will accept your small remote collaboration. Thank you.

1

I suggest you use the bootstrap grid http://getbootstrap.com/css/#grid to create the. layouts by first assembling the grids and then implementing the loops. https://codex.wordpress.org/pt-br:O_Loop

Well, I guess it’s something you have to wear:

<div class="row">
  <div class="col-sm-6">  


    <?php if (have_posts()) : ?>
      <?php $number = 0 ?>
      <?php while (have_posts()) : the_post(); ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
         

            <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><p><?php echo the_content(); ?></p></a>
          
 
        </article><!--col--> 
        <?php $number++; ?>      
      <?php endwhile; ?>
    <?php endif; ?>
  </div>  

</div>  

You define how the grid will be represented and then create the loop to bring the wordpress information

  • The tools to be used I know. I don’t know the logic of programming involving these tools to apply to the layout. Anyone have an idea? Thank you.

  • edited the answer

  • Can you refine the answer to reproduce a part of the layout so I can develop the rest? Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.