Loop wordpress with different div positions

Asked

Viewed 228 times

-1

I have a category of recipes in Wordpress appear 2 post in it. I would like each post to have a formatting.
Ex: Content to the left and image to the right and another image post to the right and content to the left but I don’t know how to do this in the wp loop. Could someone help me?

  • Confusing question. The title does not seem to have to do with the body of the question. Try to improve so we can help you

  • Thank you, I’m new here if you can help me with my situation I would appreciate it very much :)

2 answers

0

If I understand correctly you want to modify only the layout, the way the posts are displayed, if that’s right, I put an example below:

// Função de busca de posts de uma determinada categoria e limitando a quantidade de post's
function get_post_custom($id_categoria, $limite=3)
{
  $additional_loop = new WP_Query("cat=$id_categoria&posts_per_page=$limite");

  $found_posts = $additional_loop->found_posts;

  //Verifica se existem post's nesta categoria
  if (0!=$found_posts) {

    //Usaremos a frente
    $valor = 1;

    while ($additional_loop->have_posts()) : $additional_loop->the_post();

    $permalink = get_permalink();
    
    $category_id = get_cat_ID( 'Últimas notícias' ); //Aqui você insere a categoria que deseja listar

    $category_link = get_category_link( $category_id );
    
    //Para alternar o layout crie uma classe css, uma que configura os elementos para a esquerda e outra para  direita
    //Para alterar as classes podemos utilizar um verificador, acima criei uma variável e logo no final do loop eu incremento ela, para saber se o número é impar ou par criei um if, dependendo do resultado ele muda o valor da variável $class
    if($valor % 2 == 1){
      $class = "classeConfigEsquerda";
    } else {
      $class = "classeConfigDireita";
    }
    
    //Exibindo os post's, repare que foi inserida a classe na div
    echo '
    <div class="<?php echo $class; ?>">     
      <div class="news-content">
        <a href="'.$permalink.'" title="'.get_the_title().'">
          <p><strong>'.get_the_title().'</strong></p>
          <span>Ler mais</span>
        </a>
      </div>
    </div>';
    $valor++;
    endwhile;
  }
}

0

      <?php
       $i = 0;
//Faz uma consulta dos posts, recebendo o parametro post_type, que pega os 
        posts do tipo cpt
       $post_type = new WP_Query( 'post_type=cpt' );
//Verifica se encontrou posts
if ( $post_type->have_posts() ) :
    //Fz um loop nos posts
    while ( $post_type->have_posts() ) :
        $post_type->the_post();
        //Veirica o resto da divisão de $i por 2
        if ( $i % 2 == 0 ) : ?>
            <div class="post-de-um-jeito" ></div> <?php
        else: ?>
            <div class="post-de-outro-jeito"></div> <?php
        endif;
    endwhile;
endif;
   ?>

But then I’d like it to be called a certain category .

  • But the function I put up you can search for the category you want friend.

  • but how do I call these two different Ivs that I have interspersed one and the other

  • This is not an answer. You should edit your question and add this excerpt.

Browser other questions tagged

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