admin管理员组

文章数量:1414939

This question already has answers here: How to reduce the database query-es (2 answers) Closed 12 years ago.

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

<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>
This question already has answers here: How to reduce the database query-es (2 answers) Closed 12 years ago.

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

<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 edited Aug 25, 2019 at 16:52 Jan Doggen 16911 bronze badges asked Feb 27, 2013 at 1:19 BatmanBatman 1451 silver badge16 bronze badges 2
  • The answer is functionally the same as the answer to your other question here: wordpress.stackexchange/questions/88549/… Can't you take what you've learned from that question and apply it to this one? – s_ha_dum Commented Feb 27, 2013 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 Commented Feb 27, 2013 at 10:03
Add a comment  | 

1 Answer 1

Reset to default 1

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.

本文标签: wp queryReduce Database Queries in Code