The below example is your code in a loop. The post_types
array includes all the post types that you’re querying. Then, the next bit (foreach
, more info here) runs the code wrapped in curly braces ({
to }
) for each item in that post_types
-array.
post_types = array('recipes', 'value_chain', 'press', 'multimedia');
foreach($post_types as $post_type){
$query = new WP_Query ( array(
'post_type' => $post_type,
'posts_per_page' => 1
) );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="item">
<div class="row">
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
<?php if( $thumb ):?>
<div class="col-md-6 col-sm-6 col-xs-12 back">
<div class="back"
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
style="background-image: url('<?php echo $thumb['0'];?>');">
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="info">
<h3>
<a href="https://wordpress.stackexchange.com/questions/326392/<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</h3>
<hr>
<div class="excerpt"><p></p></div>
<div class="row">
<div class="col-xs-12">
<a href="https://wordpress.stackexchange.com/questions/326392/<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="read-more">
<?php echo _e('Leer más', 'myCustomTemplate'); ?>
</a>
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="col-md-12 col-sm-12 col-xs-12 no-bg">
<div class="info">
<h3>
<a href="https://wordpress.stackexchange.com/questions/326392/<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</h3>
<hr>
<div class="excerpt"><p><?php echo wp_trim_words( get_the_content(), 100 ); ?></p></div>
<div class="row">
<div class="col-xs-12">
<a href="https://wordpress.stackexchange.com/questions/326392/<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="read-more">
<?php echo _e('Leer más', 'myCustomTemplate'); ?>
</a>
</div>
</div>
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php endwhile;
endif; wp_reset_postdata();
}
?>