0
votes

I have my slider that works fine. My main question is the loop that i am using, can it more simplified?

I am populating the slider with categories which have posts attached to that with featured images. it pulls the featured image as well as the title of the post, the author and a simple read more button.

my loop with slider

<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>
1

1 Answers

1
votes

You could compress it by using a for loop.

<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>