This question already has answers here: How to reduce the database query-es (2 answers) Closed 12 years ago.admin管理员组文章数量:1414939
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'); ?>&h=240&w=418&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'); ?>&h=40&w=40&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'); ?>&h=77&w=218&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'); ?>&h=77&w=143&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'); ?>&h=240&w=418&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'); ?>&h=40&w=40&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'); ?>&h=77&w=218&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'); ?>&h=77&w=143&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
1 Answer
Reset to default 1Because 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'); ?>&h=77&w=218&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'); ?>&h=77&w=143&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
版权声明:本文标题:wp query - Reduce Database Queries in Code 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745216573a2648198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论