How to use forech in Wordpress using the Wp_query variable

Asked

Viewed 73 times

1

Good morning! I’m not able to implement a forech in a database search using the native variables of wordpress, searching found something similar however is not what I need.

<?php global $post; 
$args = array('category' => 17); 
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
...
endforeach;
?>

I want to return the data in a list to change the class from the first search to the second, example below:

<?php global $post; 
    $args = array('category' => 17); 
    $custom_posts = get_posts($args);
    foreach($custom_posts as $post) : setup_postdata($post);
    <div class="divA">
    </div>
    <div class="divB">
    </div>
    endforeach;
    ?>

Returning:

<div class="divA">
Teste 1
</div>
<div class="divB">
Teste 2
</div>
<div class="divA">
Teste 3
</div>
<div class="divB">
Teste 4
</div>

Would it be possible?

2 answers

0

In case someone needs something similar follow the final code as it was:

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    global $post; 
    $args = array('category' => 14, 'orderby' => 'rand', 'order' => 'ASC'); 
    $custom_posts = get_posts($args);
    $count = 0;
    foreach($custom_posts as $post) : setup_postdata($post);
        $post_class = ($count%2 == 0) ? 'box_sistemaEsq' : 'box_sistemaDir';
?>
             <article class="<?php echo $post_class ?>">
                <div class="box_sistemaP">
                <p class="box_sistema3">
                <a href="http://<?php echo get_field('url_do_site'); ?>" target="_blank" title="<?php the_title();?>"><?php the_post_thumbnail('foto_destacada');?></a>
                <?php echo  get_the_content(); ?>
                </p>
                <p class="box_sistema2">
                <?php the_title();?>
                </p>
                <p class="box_sistema4">
                    <a href="http://<?php echo get_field('url_do_site'); ?>" target="_blank" title="<?php the_title();?>"><?php echo get_field('url_do_site'); ?></a>
                </p>
                </div>
            </article> 
<?php
        $count++;
    endforeach;
   wp_reset_query();
?>

0


You can turn the class into a variable and create a counter, with each loop checking whether the counter is even or odd and change the class, see the example:

<?php 
    global $post; 
    $args = array('category' => 17); 
    $custom_posts = get_posts($args);
    $count = 0;
    foreach($custom_posts as $post) : setup_postdata($post);
        $post_class = ($count%2 == 0) ? 'divA' : 'divB';

        echo '<div class="' . $post_class . '">';
            ...
        echo '</div>';

        $count++;
    endforeach;
?>
  • Forgive the delay to answer, it worked perfectly just had to perform some adjustments like inserting an echo for the class to be displayed

  • I’m glad it worked out. Truth, I forgot the echo, I edited the answer.

Browser other questions tagged

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