如何减少数据库查询量

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

Hy,我为新闻制作了一张幻灯片,问题是如何减少dathabase查询。。。或者如何压缩此代码。。。

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>

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

您可以使用尝试此版本WP_Query() 仅调用一次:

<?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>
在哪里$recentPosts->rewind_posts() 用于在第二个循环之前倒带。

SO网友:Mohamed Bourribab
<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>
结束

相关推荐

WordPress存储库禁止我在非WordPress文件中调用wp-load.php

我制作了一个Wordpress插件,它是一种下载管理器。我有一个外部文件下载。负责下载的php。此文件不在Wordpress环境中,因此我必须调用:$wp_root = dirname(dirname(dirname(dirname(__FILE__)))); require_once $wp_root.\'/wp-load.php\'; 我可以使用$wpdb进行sql查询。注意:此文件必须是外部文件,因为我使用函数“headers()”强制下载。header(\"Pragma: pub

如何减少数据库查询量 - 小码农CODE - 行之有效找到问题解决它

如何减少数据库查询量

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

Hy,我为新闻制作了一张幻灯片,问题是如何减少dathabase查询。。。或者如何压缩此代码。。。

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>

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

您可以使用尝试此版本WP_Query() 仅调用一次:

<?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>
在哪里$recentPosts->rewind_posts() 用于在第二个循环之前倒带。

SO网友:Mohamed Bourribab
<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>

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请