填充滑块,带有类别特色图像的 PHP 循环


populated slider with php loop of category featured images

我的滑块工作正常。我的主要问题是我正在使用的循环,它可以简化吗?

我正在用类别填充滑块,这些类别附有带有特色图像的帖子。 它拉取特色图片以及帖子标题、作者和一个简单的阅读更多按钮。

我的带滑块的循环

<div class="slider">
    <ul class="slide">
        <li>
            <?php query_posts('showposts=1&cat=48'); ?>
                <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                    <?php echo get_the_title(); ?> 
                        <div class="latest-post">
                            <a href="<?php the_permalink() ?>" rel="bookmark">
                                <?php the_post_thumbnail();  echo '<p>read more</p>'; ?> 
                            </a>
                        </div>
                    <p>Other posts by <?php the_author_posts_link(); ?></p>             
                <?php endwhile; endif; ?>
            <?php wp_reset_query(); ?>
        </li>
        <li>
            <?php query_posts('showposts=1&cat=49'); ?>
                <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                    <?php echo get_the_title(); ?> 
                        <div class="latest-post">
                            <a href="<?php the_permalink() ?>" rel="bookmark">
                                <?php the_post_thumbnail();  echo '<p>read more</p>'; ?> 
                            </a>
                        </div>
                    <p>Other posts by <?php the_author_posts_link(); ?></p>             
                <?php endwhile; endif; ?>
            <?php wp_reset_query(); ?>
        </li>
        <li>
            <?php query_posts('showposts=1&cat=50'); ?>
                <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                    <?php echo get_the_title(); ?> 
                        <div class="latest-post">
                            <a href="<?php the_permalink() ?>" rel="bookmark">
                                <?php the_post_thumbnail();  echo '<p>read more</p>'; ?> 
                            </a>
                        </div>
                    <p>Other posts by <?php the_author_posts_link(); ?></p>             
                <?php endwhile; endif; ?>
            <?php wp_reset_query(); ?>
        </li>
        <li>
            <?php query_posts('showposts=1&cat=51'); ?>
                <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                    <?php echo get_the_title(); ?> 
                        <div class="latest-post">
                            <a href="<?php the_permalink() ?>" rel="bookmark">
                                <?php the_post_thumbnail();  echo '<p>read more</p>'; ?> 
                            </a>
                        </div>
                    <p>Other posts by <?php the_author_posts_link(); ?></p>             
                <?php endwhile; endif; ?>
            <?php wp_reset_query(); ?>
        </li>
    </ul>
</div>

您可以使用 for 循环来压缩它。

<div class="slider">
<ul class="slide">
    <?php for ($i=48;$i<52;$i++) { ?>
        <li>
            <?php query_posts('showposts=1&cat=' . $i . ''); ?>
                <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                    <?php echo get_the_title(); ?> 
                        <div class="latest-post">
                            <a href="<?php the_permalink() ?>" rel="bookmark">
                                <?php the_post_thumbnail();  echo '<p>read more</p>'; ?> 
                            </a>
                        </div>
                    <p>Other posts by <?php the_author_posts_link(); ?></p>             
                <?php endwhile; endif; ?>
            <?php wp_reset_query(); ?>
        </li>
    <?php } ?>
</ul>