我必须从Tony那里得到关于构建手风琴WordPress的帮助,但在循环中,它重复了相同的三篇帖子,我不知道如何用正确的方式进行循环,当单击类别时,我必须看到与之相关的帖子,但不会发生,它只是重复了相同的3篇帖子。
<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>
在这里,我想返回3篇帖子,但每一篇帖子都在一张卡片和正确的类别中,因此循环帖子中的错误或接近。
<?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;
?>
谢谢。