减少代码中的数据库查询

时间:2013-02-27 作者:Batman

我可以通过压缩这个来减少查询吗?,我可以减少查询吗?如果可以,我该怎么做?

注:图像大小不同

enter image description here

<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>

1 个回复
最合适的回答,由SO网友:birgire 整理而成

因为这与

How to reduce the database query-es

然后,我将为您概述一个可能的解决方案:

有四个类似的循环

1) 1 post && offset=0 
2) 4 posts && offset=1 
3) 5 posts && offset=1 
4) 6 posts && offset=1 
因此,您的主要查询将是

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

$recentPosts = new WP_Query($args);
使用该选项:

  • $recentPosts->current_post 给你这个职位(0,1,...) 在循环中$recentPosts->rewind_posts() 倒回帖子,
  • $recentPosts->next_post() 增加位置,
您可以使用4个立柱和偏移量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; ?>

<?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; ?>

<?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; ?>
您也可以在其他循环中使用此选项,在这些循环中,您可以更改if-满足您的需求。

ps: 最后,您应该使用

<?php wp_reset_postdata(); ?>
恢复全局$post 到主查询中的当前帖子,因为它被上面的循环更改了。

更新:

将此项放在div 密码

<?php
$args=array( 
    \'post_type\' => \'stiri\',
    \'posts_per_page\' => 7,
    \'taxonomy\' => \'stire\',
    \'stire\' => \'articole-speciale\'
);
$recentPosts = new WP_Query($args);
?>
div结构中有一些奇怪的地方,所以我给您举两个例子:

您可以将此用作dbr 分区块:

<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>
类似地dbm 分区块:

<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>
然后,您可以对其他两种情况执行类似的操作。希望这有帮助。

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post