Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hy, i make a slide for news, the question is how can i reduce the dathabase queries...ore how can i compress this code...

http://s16.postimage.org/bswe0vrph/queries.jpg

<div id="featured" >
<ul class="ui-tabs-nav">

<li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-1">
<?php
$recentPosts = new WP_Query();
$recentPosts->query(array( 
    'post_type' => 'stiri',
    'showposts' => 1,
    'offset'=>0,
    'taxonomy' => 'stire',
    'stire' => 'articole-speciale'
    )
);
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<a href="#fragment-1"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=50&amp;w=80&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /><span><?php the_title(); ?></span></a>
<?php endwhile; ?>
</li>

<li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-2">
<?php
$recentPosts = new WP_Query();
$recentPosts->query(array( 
    'post_type' => 'stiri',
    'showposts' => 1,
    'offset'=>1,
    'taxonomy' => 'stire',
    'stire' => 'articole-speciale'
    )
);
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<a href="#fragment-2"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=50&amp;w=80&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /><span><?php the_title(); ?></span></a>
<?php endwhile; ?>
</li>

<li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-3">
<?php
$recentPosts = new WP_Query();
$recentPosts->query(array( 
    'post_type' => 'stiri',
    'showposts' => 1,
    'offset'=>2,
    'taxonomy' => 'stire',
    'stire' => 'articole-speciale'
    )
);
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<a href="#fragment-3"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=50&amp;w=80&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /><span><?php the_title(); ?></span></a>
<?php endwhile; ?>
</li>

<li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-4">
<?php
$recentPosts = new WP_Query();
$recentPosts->query(array( 
    'post_type' => 'stiri',
    'showposts' => 1,
    'offset'=>3,
    'taxonomy' => 'stire',
    'stire' => 'articole-speciale'
    )
);
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<a href="#fragment-4"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=50&amp;w=80&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /><span><?php the_title(); ?></span></a>
<?php endwhile; ?>
</li>

<li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-5">
<?php
$recentPosts = new WP_Query();
$recentPosts->query(array( 
    'post_type' => 'stiri',
    'showposts' => 1,
    'offset'=>4,
    'taxonomy' => 'stire',
    'stire' => 'articole-speciale'
    )
);
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<a href="#fragment-5"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=50&amp;w=80&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /><span><?php the_title(); ?></span></a>
<?php endwhile; ?>
</li>
          </ul>

        <!-- Stirea 1  -->
        <div id="fragment-1" class="ui-tabs-panel" style="">
        <?php
        $recentPosts = new WP_Query();
        $recentPosts->query(array( 
       'post_type' => 'stiri',
       'showposts' => 1,
       'taxonomy' => 'stire',
       'stire' => 'articole-speciale'
                )
            );
        ?>
        <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
            <a href="<?php the_permalink() ?>"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=325&amp;w=664&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a>
             <div class="info" >
                <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                <p><?php $excerpt = get_the_excerpt();echo string_limit_words($excerpt,30);?><a class="detalii" href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Stire %s'), the_title_attribute('echo=0')); ?>">...detalii</a></p>
             </div>
        <?php endwhile; ?>
        </div>

        <!-- Stirea 2  -->
        <div id="fragment-2" class="ui-tabs-panel ui-tabs-hide" style="">
        <?php
        $recentPosts = new WP_Query();
        $recentPosts->query(array( 
       'post_type' => 'stiri',
       'showposts' => 1,
       'offset'=>1,
       'taxonomy' => 'stire',
       'stire' => 'articole-speciale'
                )
            );
        ?>
        <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
            <a href="<?php the_permalink() ?>"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=325&amp;w=664&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a>
             <div class="info" >
                <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                <p><?php $excerpt = get_the_excerpt();echo string_limit_words($excerpt,30);?><a class="detalii" href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Stire %s'), the_title_attribute('echo=0')); ?>">...detalii</a></p>
             </div>
        <?php endwhile; ?>
        </div>

        <!-- Stirea 3  -->
        <div id="fragment-3" class="ui-tabs-panel ui-tabs-hide" style="">
        <?php
        $recentPosts = new WP_Query();
        $recentPosts->query(array( 
       'post_type' => 'stiri',
       'showposts' => 1,
       'offset'=>2,
       'taxonomy' => 'stire',
       'stire' => 'articole-speciale'
                )
            );
        ?>
        <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
            <a href="<?php the_permalink() ?>"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=325&amp;w=664&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a>
             <div class="info" >
                <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                <p><?php $excerpt = get_the_excerpt();echo string_limit_words($excerpt,30);?><a class="detalii" href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Stire %s'), the_title_attribute('echo=0')); ?>">...detalii</a></p>
             </div>
        <?php endwhile; ?>
        </div>

        <!-- Stirea 4  -->
        <div id="fragment-4" class="ui-tabs-panel ui-tabs-hide" style="">
        <?php
        $recentPosts = new WP_Query();
        $recentPosts->query(array( 
       'post_type' => 'stiri',
       'showposts' => 1,
       'offset'=>3,
       'taxonomy' => 'stire',
       'stire' => 'articole-speciale'
                )
            );
        ?>
        <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
            <a href="<?php the_permalink() ?>"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=325&amp;w=664&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a>
             <div class="info" >
                <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                <p><?php $excerpt = get_the_excerpt();echo string_limit_words($excerpt,30);?><a class="detalii" href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Stire %s'), the_title_attribute('echo=0')); ?>">...detalii</a></p>
             </div>
        <?php endwhile; ?>
        </div>

        <!-- Stirea 5  -->
        <div id="fragment-5" class="ui-tabs-panel ui-tabs-hide" style="">
        <?php
        $recentPosts = new WP_Query();
        $recentPosts->query(array( 
       'post_type' => 'stiri',
       'showposts' => 1,
       'offset'=>4,
       'taxonomy' => 'stire',
       'stire' => 'articole-speciale'
                )
            );
        ?>
        <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
            <a href="<?php the_permalink() ?>"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=325&amp;w=664&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a>
             <div class="info" >
                <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                <p><?php $excerpt = get_the_excerpt();echo string_limit_words($excerpt,30);?><a class="detalii" href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Stire %s'), the_title_attribute('echo=0')); ?>">...detalii</a></p>
             </div>
        <?php endwhile; ?>
        </div>

</div>
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You can try this version with WP_Query() called only once:

<?php
$args=array( 
    'post_type' => 'stiri',
    'posts_per_page' => 5,
    'taxonomy' => 'stire',
    'stire' => 'articole-speciale'
    );
$recentPosts = new WP_Query($args);
?>
<div id="featured" >
    <ul class="ui-tabs-nav">
        <?php $i=0; while ($recentPosts->have_posts()) : $recentPosts->the_post(); $i++;?>
            <li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-<?php echo $i;?>">
                <a href="#fragment-<?php echo $i;?>"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=50&amp;w=80&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /><span><?php the_title(); ?></span></a>
            </li>
        <?php endwhile; ?>
    </ul>

    <?php $recentPosts->rewind_posts();?>

    <?php $i=0; while ($recentPosts->have_posts()) : $recentPosts->the_post(); $i++; ?>    
        <!-- Stirea <?php echo $i;?>  -->
        <div id="fragment-<?php echo $i;?>" class="ui-tabs-panel" style="">
            <a href="<?php the_permalink() ?>"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=325&amp;w=664&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a>
            <div class="info" >
                <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                <p><?php $excerpt = get_the_excerpt();echo string_limit_words($excerpt,30);?><a class="detalii" href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Stire %s'), the_title_attribute('echo=0')); ?>">...detalii</a></p>
            </div>
        </div>
    <?php endwhile; ?>
</div>

where $recentPosts->rewind_posts() is used to rewind before the second loop.

share|improve this answer
    
Thanks it works! –  Batman Feb 27 '13 at 1:13
    
Birgire can you look at this and tell me if i can ... wordpress.stackexchange.com/questions/88572/… –  Batman Feb 27 '13 at 1:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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