I have to get help from Tony about build accordion WordPress but in the loop, its repeated the same three posts and I don't know how to make a loop with the right way when clicking on category I must see the post relative to it but it doesn't happen it just repeats the same 3 posts.
<ul class="s3_accordion">
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'DESC'
) );
$cat_array = array();
foreach( $categories as $category ) {
$category_link = sprintf(
'<li class="artical_options" data-class=".%2$s" alt="%2$s">%3$s</li>',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( '%s', 'textdomain' ), $category->slug ) ),
esc_html( $category->slug )
);
echo sprintf( esc_html__( '%s', 'textdomain' ), $category_link );
$cat_array[] = $category->term_id;
}
?>
</ul>
and here I want to return 3 posts but every post in one card and right categories, so the error in the loop posts or something close.
<?php
if( !empty( $cat_array ) ) :
foreach( $cat_array as $cat ) :
$category = get_term( $cat, 'category' );
$cat_slug = $category->slug;
echo '<div class="col-lg-4 s3_shuffle_image ' . $cat_slug . '">';
$postslist = get_posts( array(
'posts_per_page' => 3,
'cat' => $cat->ID,
'order' => 'DESC'
) );
if( $postslist ) :
foreach( $postslist as $post ) :
setup_postdata( $post ); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php
endforeach;
wp_reset_postdata();
endif;
echo '</div>';
endforeach;
endif;
?>
thanks.
$post
use some other variable there.$post
is a global variable and in many cases using it create conflicts.foreach($postslist as $var): $id=$var->ID;
and thenget_the_title($id)
instead ofthe_title()
;get_permalink($id)
instead ofthe_permalink()