Query_Posts的多个实例

时间:2013-08-01 作者:Phantasmix

我需要在一页上有8个这样的。每个帖子列表将位于其自己的编号开关选项卡中。

有没有更好、更有效的方法?

<div id="tab1">
<div class="content">
    <ul>
        <?php
            // The Query
            query_posts( array ( \'category_name\' => \'CV\', \'posts_per_page\' => -1 ) );

            // The Loop
            while ( have_posts() ) : the_post(); ?>

            <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
            <p><?php the_content(); ?></p>
        <?php endwhile;

            // Reset Query
            wp_reset_query();            
        ?>
    </ul>
</div><!-- / .content -->
</div><!-- / #tab1 -->
编辑以添加最终代码:

<?php
// getting only children of category ID 16
$categories = get_categories(\'child_of=16\');
// do not need to check for empty categories
{
?>

<ul class="tabs">
<?php foreach ($categories as $c ) { ?>
    <li>
      <a href="#tab<?php echo $c->term_id; ?>">
        <img src="<?php bloginfo(\'template_directory\'); ?>/images/logo-<?php echo $c->slug; ?>.png" alt="<?php echo $c->name; ?>" />
      </a>
    </li>
<?php } ?>
</ul> <!-- / .tabs -->

<?php } ?>

<?php { foreach ($categories as $c ) { ?>
<div id="tab<?php echo $c->term_id; ?>">
<a href="#" class="left-button"><img src="prev.png" alt="Prev" /></a>
<div class="content">
    <?php
    $q = new WP_Query( array( \'posts_per_page\' => -1, \'cat\' => $c->term_id ) );
    if ( $q->have_posts() ) : ?> <ul class="tabs-content"> <?php while( $q->have_posts() ) : $q->the_post();
    ?>
    <li>
      <h3>
      <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
      <?php the_title(); ?></a>
      </h3>
      <?php the_content(); ?>
    </li>
    <?php endwhile; ?> </ul> <?php endif; wp_reset_postdata(); ?>
</div><!-- / .content -->
<a href="#" class="right-button"><img src="next.png" alt="Next" /></a>
</div><!-- / #tab<?php echo $c->term_id; ?> -->
<?php } } ?>

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

如果有8个类别,则需要8个查询。改进代码使用WP_Query 而不是query_posts:

<?php
$categories = get_categories();
if ( ! empty($categories) ) {
?>
<ul class="tabs">
<?php foreach ($categories as $c ) { ?>
<li>
  <a href="#tab<?php echo $c->term_id; ?>">
    <img src="<?php bloginfo(\'template_directory\'); ?>/images/logo-<?php echo $c->slug; ?>.png" alt="<?php echo $c->name; ?>" />
  </a>
</li>
<?php } ?>
</ul> <!-- / .tabs -->
<?php } ?>

<?php if ( ! empty($categories) ) { foreach ($categories as $c ) { ?>
<div id="tab<?php echo $c->term_id; ?>">
<a href="#" class="left-button"><img src="prev.png" alt="Prev" /></a>
<div class="content">
<?php
$q = new WP_Query( array( \'posts_per_page\' => -1, \'cat\' => $c->term_id ) );
if ( $q->have_posts() ) : ?> <ul> <?php while( $q->have_posts() ) : $q->the_post();
?>
<li>
  <h3>
  <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
  <?php the_title(); ?></a>
  </h3>
  <p><?php the_content(); ?></p>
</li>
<?php endwhile; ?> </ul> <?php endif; wp_reset_postdata(); ?>
</div><!-- / .content -->
<a href="#" class="right-button"><img src="next.png" alt="Next" /></a>
</div><!-- / #tab<?php echo $c->term_id; ?> -->
<?php } } ?>

结束

相关推荐