最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How do loop categories post from according in WordPress? is it impossible to solve this problem?

programmeradmin3浏览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.

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.

Share Improve this question edited Apr 20, 2020 at 11:11 Long Way asked Apr 20, 2020 at 8:30 Long WayLong Way 72 bronze badges 4
  • Do not use $post use some other variable there. $post is a global variable and in many cases using it create conflicts. – BlueSuiter Commented Apr 20, 2020 at 8:51
  • @BlueSuiter i change variable it's the same problem, thanks for you comment – Long Way Commented Apr 20, 2020 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 Commented Apr 20, 2020 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 Commented Apr 20, 2020 at 11:05
Add a comment  | 

1 Answer 1

Reset to default 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;

发布评论

评论列表(0)

  1. 暂无评论