将WordPress循环拆分成类别和相关帖子的列

时间:2011-08-03 作者:Studioleland

以下是我需要对“归档”页面上的5列执行的操作:

我已经可以在一个循环中遍历类别和帖子,以很好地显示它们,我只是不知道如何将其拆分为列。我还需要分页。基本上每页只显示18篇文章的标题/摘录。有什么帮助吗?我的循环如下:

UPDATE: I am posted the working solution below. This works perfectly to break a continuous loop of posts sorted and displayed by category of any type or taxonomy.

WHAT STILL NEEDS WORK: 我似乎不知道如何使寻呼工作。我需要分页来计算总帖子数(忽略类别),并且每页仅显示12个左右。我添加了分页代码,但它被注释掉了。

<?php
// for a given post type, return all
        $post_type = \'retailers\';
        $tax = \'states\';
        $tax_terms = get_terms($tax);
        $post_counter = 0; // reset so we can generate columns 
        $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;

            if ($tax_terms) { ?>

            <ul class="column_wrap" style="margin:0 10px; padding:0; width:165px; float:left;">

            <?php foreach ($tax_terms as $tax_term) {
                 $args=array(
                \'post_type\' => $post_type,
                "$tax" => $tax_term->slug,
                \'post_status\' => \'publish\',
                \'posts_per_page\' => -1,
              //\'posts_per_page\' => 15,
              //\'paged\' => $paged,
                \'orderby\' => \'title\',
                \'order\' => \'ASC\',
                \'caller_get_posts\'=> 1
                ); // END $args

                $my_query = null; // clear the query variable
                $my_query = new WP_Query($args);

                if( $my_query->have_posts() ) { ?>

                      <li class="column_row" style="float:left;">

                        <div class="retailer_state_wrap" style="width:165px; float:left;">
                            <h4><?php echo $tax_term->name; ?></h4>
                        </div>

                      </li>

        <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
                      <li class="column_row" style="float:left;">

                          <div class="retailer_wrap retailer_id-<?php the_ID(); ?>" style="padding:0; width:165px; float:left;">
                            <p>
                              <span class="retailer_title"><?php the_title(); ?></span><br />
                              <span class="retailer_address">
                                <?php if (get_post_meta($post->ID, \'_simple_fields_fieldGroupID_1_fieldID_2_numInSet_0\', true)) { ?>
                                  <?php echo get_post_meta($post->ID, \'_simple_fields_fieldGroupID_1_fieldID_2_numInSet_0\', true); ?>
                                <?php } ?>
                                <?php echo " "; ?>
                                <?php if (get_post_meta($post->ID, \'_simple_fields_fieldGroupID_1_fieldID_1_numInSet_0\', true)) { ?>
                                  <?php echo get_post_meta($post->ID, \'_simple_fields_fieldGroupID_1_fieldID_1_numInSet_0\', true); ?>
                                <?php } ?>
                              </span><br />
                              <span class="retailer_phone">                               
                                <?php if (get_post_meta($post->ID, \'_simple_fields_fieldGroupID_2_fieldID_3_numInSet_0\', true)) {
                                  echo get_post_meta($post->ID, \'_simple_fields_fieldGroupID_2_fieldID_3_numInSet_0\', true); ?>
                                <?php } ?>
                              </span>    
                            </p>                                          
                          </div>
                          <?php $post_counter++; ?>
                        </li>

                        <?php if ( 0 == $post_counter % 7 ) { ?>
                          </ul>
                          <ul class="column_wrap" style="margin:0 10px; padding:0; width:165px; float:left;">
                        <?php } ?>
                      <?php
                      endwhile;
                      //wp_pagenavi( array( \'query\' => $my_query ) ); 
                    } // END if have_posts loop
                    wp_reset_query();

                  } // END foreach $tax_terms
                } // END if $tax_terms
                ?>

1 个回复
SO网友:Tristar Web Design

您需要做的是遍历一个类别ID数组。例如:

$categories = get_categories();
然后循环浏览类别,并在query\\u帖子中使用类别id:

foreach($categories as $category) {
    $args = array(
    \'cat\' = $category->ID
    )
    // return your loop here...
}
或者别的什么。。。假设我理解你的问题。

结束

相关推荐

How do I order my loop query?

我的数据库中有一个表,其中包含我试图排序帖子的投票统计。屏幕截图:the table我尝试过这样的方法,它可以显示所有投票的帖子和分页,但不会按DESC排序。如果我接近这一权利,任何建议都将不胜感激。 <?php global $wpdb; $my_posts = $wpdb->get_col(\"SELECT like_pid FROM wp_likes_count ORDER BY like_count\"); $paged = (get_query_var(