Loop di post divisi per categoria

Loop di post divisi per categoria 2

Con questo snippet PHP potrete prendere qualsiasi tipologia di post suddivisi per categoria, torna utile se dovete dividere i post in piu sezioni e dividerli per categoria.

<?php
$categories = get_terms( 'category' );
foreach ( $categories as $category ) {?>
<?php $cat_id = $category->term_id;?>
<?php $term_slug = $category->slug; ?>
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => $cat_id,
'field' => 'term_id'
)
)
);
?>
<div class="row_title" id="<?php echo $term_slug ?>">
<div class="content">
<h2><?php echo $category->name;?></h2>
</div>
</div>
<div class="content">
<?php $posts = get_posts($args); ?>
<?php foreach($posts as $post) { ?>
<div class="product">
<h3 class="title_post"><?php the_title(); ?></h3>
<a class="" href="<?php the_permalink(); ?>">
<?php global $post; ?>
<?php $post_id = $post->ID; ?>
<div class="post__img">
<?php the_post_thumbnail('medium'); ?>
</div>
</a>
</div>
<?php } ?>
</div>
<?php } ?>

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *