0

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.

  • Do not use $post use some other variable there. $post is a global variable and in many cases using it create conflicts. – BlueSuiter Apr 20 at 8:51
  • @BlueSuiter i change variable it's the same problem, thanks for you comment – Long Way Apr 20 at 9:01
  • foreach($postslist as $var): $id=$var->ID; and then get_the_title($id) instead of the_title(); get_permalink($id) instead of the_permalink() – BlueSuiter Apr 20 at 10:39
  • @BlueSuiter thanks again for replay but can just try code in your pc to see what i have here, the problem i think it in the get post data the idea get all post and switch between it under his categories . i hope you get the idea – Long Way Apr 20 at 11:05
0

Thanks all for help here the link about solved it

bcworkz (@bcworkz)

37 minutes ago

There are two issues I see. $cat_array is an array of IDs, not objects, so the ‘cat’ query arg should just be 'cat' => $cat,

The other is when you use setup_postdata( $post ), you must explicitly declare global $post;

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.