Can anyone explain this logic to me in Wordpress?

Asked

Viewed 93 times

0

Good saw this logic in Wordpress theme developed by a colleague, and there was the following logic:

<?php
    $i = 0;
    $post_type = new WP_Query( 'post_type=cpt' );
    if ( $post_type->have_posts() ) :
        while ( $post_type->have_posts() ) :
            $post_type->the_post();
            if ( $i % 2 == 0 ) : ?>
                <div class="post-de-um-jeito" ></div> <?php
            else: ?>
                <div class="post-de-outro-jeito"></div> <?php
            endif;
        endwhile;
    endif;
?>

I know the logic is simple, but I’m new to PHP, and I’ve never seen it in Wordpress. Thank you!

  • 2

    the operator module % returns the rest of the entire division, so the code displays the <div>s pairs one way and odd another.

  • Picks up posts like cpt; If you have posts; While you have posts; Print the post; If the post is par printa div.post-de-um-jeito if it doesn’t print div.post-de-outro-jeito... +- that ?

  • Got it... Very nice guys thanks for the force :)

2 answers

3


This logic is also part of PHP, but the difference in Wordpress is that he created his own methods for each action with the database, using a class:

    <?php
        //declara a variável $i como 0 para no while ele trazer a função de mod que explicarei abaixo
        $i = 0;
        /* Cria uma instância da classe de Querys do Wordpress,
           passando para o método construtor o parâmetro da consulta,
           que no caso é post_type=cpt, siginifica que o tipo
           "custom post type" (tipo de post customizado) */
        $post_type = new WP_Query( 'post_type=cpt' );
        //Então ele verifica se o método "have_posts()" possui algum post
        if ( $post_type->have_posts() ) :
            //Se ele retornar "true", ele faz um laço do tipo "while" no objeto para listar cada valor retornado 
            while ( $post_type->have_posts() ) :
 // aqui ele executa o método "the_post()", que internamente tem toda a estrutura da publicação, como título, texto, data etc.
                $post_type->the_post();
                //Aqui ele pega o resto da divisão de $i por 2, caso seja igual a 0, ele exibe abaixo do post uma estrutura de um jeito, caso contrário de outro jeito.
                if ( $i % 2 == 0 ) : ?>
                    <div class="post-de-um-jeito" ></div> <?php
                else: ?>
                    <div class="post-de-outro-jeito"></div> <?php
                endif;
              //aqui deveria ter um $i++;
            endwhile;
        endif;
    ?>

Basically this structure repeats itself by alternating the footer of each post for each post that is displayed in the loop. But I believe that for this rule with the $i working should have just below, before the endwhile an incrementer: $i++;

0

I put the commented code:

<?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 from what I understood of this code he wanted to cross-reference the posts with odd number and even using the counter $i, however it is not incrementing this variable. In the case $i will always be 0 and will always enter the if ( $i % 2 == 0 ).

Browser other questions tagged

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