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.

This question already has an answer here:

Can i reduce the queries by compresing this ?, cand i reduce queries, if i can , how can i do?

Note : The images are in diferent size

http://oi55.tinypic.com/rwp3ex.jpg

<div class="art-Block">
    <div class="art-Block-tl"></div>
    <div class="art-Block-tr"></div>
    <div class="art-Block-bl"></div>
    <div class="art-Block-br"></div>
    <div class="art-Block-tc"></div>
    <div class="art-Block-bc"></div>
    <div class="art-Block-cl"></div>
    <div class="art-Block-cr"></div>
    <div class="art-Block-cc"></div>
    <div class="art-Block-body">

<div class="index-categorie">Stiri din lumea cinematografiei</div>    
<div class="index-linie"></div>

<div id="dbs">
<div class="dbl">
<?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="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php $excerpt = the_title();echo string_limit_words($excerpt,5);?></a>
<div class="dbli"><a href="<?php the_permalink() ?>"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=240&amp;w=418&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a></div>
<div class="dblc">
<?php $excerpt = get_the_excerpt();echo string_limit_words($excerpt,80);?><a class="detalii" href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Stire %s'), the_title_attribute('echo=0')); ?>">...detalii</a>
<?php endwhile; ?>
</div>

<?php
$recentPosts = new WP_Query();
$recentPosts->query(array( 
    'post_type' => 'stiri',
    'showposts' => 6,
    'offset'=>1,
    'taxonomy' => 'stire',
    'stire' => 'articole-speciale'
    )
);
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div class="dbls">
<div class="dblsi"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=40&amp;w=40&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></div>
<div class="dblst"><a href="<?php the_permalink() ?>"><?php $mytitle = get_the_title(); if ( strlen($mytitle) >60 ) $mytitle = substr($mytitle,0,60); echo $mytitle; ?></a></div>
</div>
<?php endwhile; ?>
</div>


<div class="dbr">
<?php
$recentPosts = new WP_Query();
$recentPosts->query(array( 
    'post_type' => 'stiri',
    'showposts' => 5,
    'offset'=>1,
    'taxonomy' => 'stire',
    'stire' => 'articole-speciale'
    )
);
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div class="dbrs">
<div class="dbrsi"><a href="<?php the_permalink() ?>"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=77&amp;w=218&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a></div>
<div class="dbrst"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div>
</div>
<?php endwhile; ?>
</div>

<div id="dbm">
<?php
$recentPosts = new WP_Query();
$recentPosts->query(array( 
    'post_type' => 'stiri',
    'showposts' => 4,
    'offset'=>1,
    'taxonomy' => 'stire',
    'stire' => 'articole-speciale'
    )
);
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div class="dbmc">
<div class="dbmci"><a href="<?php the_permalink() ?>"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=77&amp;w=143&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a></div>
<div class="dbmct"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div>
</div>
<?php endwhile; ?>
</div>
</div>

</div>
</div>
share|improve this question

marked as duplicate by s_ha_dum, brasofilo, Wyck, Tom J Nowell, toscho Feb 27 '13 at 14:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
The answer is functionally the same as the answer to your other question here: wordpress.stackexchange.com/questions/88549/… Can't you take what you've learned from that question and apply it to this one? –  s_ha_dum Feb 27 '13 at 1:24
    
ok thanks , i'will give a try.. Thanks –  Batman Feb 27 '13 at 1:24
    
Compressing a file does not change what it does, I fail to see what compression has to do with query calling –  Tom J Nowell Feb 27 '13 at 10:03

1 Answer 1

up vote 1 down vote accepted

Because this is very similar to

How to reduce the database query-es

I will then give you an outline of a possible solution:

You have four similar loops with

1) 1 post && offset=0 
2) 4 posts && offset=1 
3) 5 posts && offset=1 
4) 6 posts && offset=1 

so your main query will be

$args=array( 
    'post_type' => 'stiri',
    'posts_per_page' => 7,
    'taxonomy' => 'stire',
    'stire' => 'articole-speciale'
);

$recentPosts = new WP_Query($args);

Using that:

  • $recentPosts->current_post gives you the position (0,1,...) in the loop,
  • $recentPosts->rewind_posts() rewinds the posts,
  • $recentPosts->next_post() increments the position,

you can try out this structure for your loop with 4 posts and offset 1:

<?php $recentPosts->rewind_posts();?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <?php if($recentPosts->current_post>0 && $recentPosts->current_post<5):?>
      ...   
    <?php endif; ?>
<?php endwhile; ?>

or

<?php $recentPosts->rewind_posts();?>
<?php $recentPosts->next_post(); // to get offset 1 ?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <?php if($recentPosts->current_post<5):?>
      ...   
    <?php endif; ?>
<?php endwhile; ?>

or

<?php $recentPosts->rewind_posts();?>
<?php $recentPosts->current_post=0; // to get offset 1 ?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <?php if($recentPosts->current_post<5):?>
      ...   
    <?php endif; ?>
<?php endwhile; ?>

You can use this on the other loops as well, where you change the if-condition to your needs.

ps: Finally you should use

<?php wp_reset_postdata(); ?>

to restore the global $post to the current post in the main query, since it was altered with the above loops.

Update:

Place this before your div code

<?php
$args=array( 
    'post_type' => 'stiri',
    'posts_per_page' => 7,
    'taxonomy' => 'stire',
    'stire' => 'articole-speciale'
);
$recentPosts = new WP_Query($args);
?>

There is something strange in your div structure, so I give you these two examples:

You can use this as your dbr div block:

<div class="dbr">
<?php $recentPosts->rewind_posts();?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <!-- #5 posts / offset=1 --->
    <?php if($recentPosts->current_post >= 1 && $recentPosts->current_post <= 5):?>
        <div class="dbrs">
            <div class="dbrsi"><a href="<?php the_permalink() ?>"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=77&amp;w=218&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a></div>
            <div class="dbrst"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div>
        </div>
    <?php endif; ?>     
<?php endwhile; ?>
</div>

and similarly for the dbm div block:

<div id="dbm">
<?php $recentPosts->rewind_posts();?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <!-- #4 posts / offset=1 --->
    <?php if($recentPosts->current_post >= 1 && $recentPosts->current_post <= 4):?>
        <div class="dbmc">
            <div class="dbmci"><a href="<?php the_permalink() ?>"><img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&amp;h=77&amp;w=143&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a></div>
            <div class="dbmct"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div>
        </div>
    <?php endif; ?>     
<?php endwhile; ?>
</div>

you can then do similarly for the other two cases. Hope this helps.

share|improve this answer
    
im soryy if i duplicate.. @birgire can you help me to get the code running.. i canot make it work... thanks in advance –  Batman Mar 11 '13 at 7:36
    
Hi, I updated the answer. –  birgire Mar 11 '13 at 12:49
    
it helps.. Thanks!! –  Batman Mar 11 '13 at 17:11

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