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

2 Answers 2

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
<div id="featured" >
  <ul class="ui-tabs-nav">

  <?php
    // shange 1 --> category ID 
    $cat=1;
    $recentPosts = new WP_Query();
    $recentPosts->query("showposts=1&cat=$cat");
    while ($recentPosts->have_posts()) : $recentPosts->the_post(); 
  ?>

  <li class="ui-tabs-nav-item ui-tabs-selected" id="nav-fragment-<?php the_ID(); ?>">
    <a href="#fragment-<?php the_ID(); ?>">
      <img src="<?php $values = get_post_custom_values("slider"); echo $values[0]; ?>"  alt="" width="80px" height="50px" />
      <span><?php the_title();?></span>
    </a>
  </li>

  <?php endwhile; ?>

  <?php 
    $recentPosts = new WP_Query( array( 'offset' => 2 ) );
    $recentPosts->query("showposts=3&cat=$cat&offset=1");
    while ($recentPosts->have_posts()) : $recentPosts->the_post(); 
  ?>  

  <li class="ui-tabs-nav-item" id="nav-fragment-<?php the_ID(); ?>">
    <a href="#fragment-<?php the_ID(); ?>">
      <img src="<?php $values = get_post_custom_values("slider"); echo $values[0]; ?>"  alt="" width="80px" height="50px" />
      <span><?php the_title();?></span>
    </a>
  </li>

  <?php endwhile; ?>

  </ul>

  <!-- First Content -->

  <?php
    // shange 1 --> category ID 
    $cat=1;
    $recentPosts = new WP_Query();
    $recentPosts->query("showposts=1&cat=$cat");
    while ($recentPosts->have_posts()) : $recentPosts->the_post(); 
  ?>

  <div id="fragment-<?php the_ID(); ?>" class="ui-tabs-panel" style="">
    <img src="<?php $values = get_post_custom_values("slider"); echo $values[0]; ?>"  width="400px" height="250px" alt="" />
    <div class="info" >
      <a class="hideshow" href="#" >Hide</a>
      <h2><a href="#" ><?php the_title();?></a></h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla tincidunt condimentum lacus. Pellentesque ut diam....<a href="#" >read more</a></p>
    </div>
  </div>

  <?php endwhile; ?>

  <!-- Second Content -->

  <?php 
    $recentPosts = new WP_Query( array( 'offset' => 2 ) );
    $recentPosts->query("showposts=3&cat=$cat&offset=1");
    while ($recentPosts->have_posts()) : $recentPosts->the_post(); 
  ?>  

  <div id="fragment-<?php the_ID(); ?>" class="ui-tabs-panel ui-tabs-hide" style="">
    <img src="<?php $values = get_post_custom_values("slider"); echo $values[0]; ?>" width="400px" height="250px"  alt="" />
    <div class="info" >
      <a class="hideshow" href="#" >Hide</a>
      <h2><a href="#" ><?php the_title();?></a></h2>
      <p>Vestibulum leo quam, accumsan nec porttitor a, euismod ac tortor. Sed ipsum lorem, sagittis non egestas id, suscipit....<a href="#" >read more</a></p>
    </div>
  </div>

  <?php endwhile; ?>

</div>
share|improve this answer
1  
Please provide an explanation to what your code does and how it works –  Pieter Goosen Jul 6 at 7:11

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.