使用多个分类对自定义帖子进行排序

时间:2012-10-27 作者:jsumnersmith

我有一个帖子类型,包括(1)位置和(2)一周中的一天的分类。

我使用第一种分类法将帖子分为多个组。下面是我使用的循环:

<?php 
$terms = get_terms(\'cell-locations\');
$argv = array(
                \'orderby\'       =>  \'by_term\',
                \'hide_empty\'    => false
                );
foreach ($terms as $term) {
  $wpq = array (\'taxonomy\'=>\'cell-locations\',\'term\'=>$term->slug);
  $myquery = new WP_Query ($wpq);
  $article_count = $myquery->post_count;
  echo \'<div class="accordionButton">\';
  echo "<h2 class=\\"cellHeader\\" id=\\"".$term->slug."\\">";
  echo $term->name;
  echo "</h2>";
  echo \'</div>\';
  echo \'<div class="accordionContent">\';
  if ($article_count) {
     echo "<ul class=\'cell_list\'>";
     while ($myquery->have_posts()) : $myquery->the_post();?>
                            <li class="cell-item">
                                <ul class="cell-list">
                                    <li><?php $terms_as_text = get_the_term_list( $post->ID, \'cell-days\', \'\', \', \', \'\' ) ;
                                        echo strip_tags($terms_as_text);
                                    ?> </li>
                                    <li> <? echo get_post_meta(get_the_ID(), \'_cell_leader\', true); ?> / <?php echo get_post_meta(get_the_ID(), \'_cell_apprentice\', true)?></li>
                                    <li>Get in touch with <a href="mailto:<?php echo get_post_meta(get_the_ID(), \'_cell_leader_email\', true);?>"><?php echo get_post_meta(get_the_ID(), \'_cell_leader\', true);?></a></li>
                                </ul>
                            </li>

     <?php endwhile;
     echo "</ul>";
  }
  echo \'</div>\';
}
?>
这为我提供了一个基于分类术语“细胞位置”的手风琴式布局这一切都很好,只是我现在想根据其他分类法“cells days”对每个位置内的帖子进行排序我用插件给他们排序(http://wordpress.org/extend/plugins/taxonomy-terms-order/).插件的api提供以下查询参数来按顺序调用帖子:

 $argv = array(
                \'orderby\'       =>  \'term_order\',
                \'hide_empty\'    => false
                );
get_terms(\'category\', $argv);
在第一个循环中创建第二个循环时遇到问题。有什么想法或建议吗?

1 个回复
SO网友:A.J. Bale

您的部分问题可能是因为您使用的是get_terms() 作用根据the docs for this function:

从4.5.0开始,分类法应该通过$args数组中的\'taxonomy\'参数传递:

$terms = get_terms( array(
    \'taxonomy\' => \'post_tag\',
    \'hide_empty\' => false,
) );
因此,我们可以重写您的get_terms()$argv 对于cells-days 分类法如下:

$argv = array(
    \'taxonomy\' => \'cells-days\',
    \'orderby\' => \'by_term\',
    \'hide_empty\' => false
);
$cells_days_terms = get_terms( $argv );
然后您可以创建另一个foreach 在第一个<li> 中的元素ul.cell-list (至少这似乎是您试图循环通过的地方cells-days):

<ul class="cell-list">
    <li>
        <?php $i_max = count( $cells_days_terms ); $i = 0; foreach ( $cells_days_terms as $cells_days_term ): $i++; ?>
            <span class="cells-days"><?= ( $i < $i_max ? $cells_days_term->name . \', \' : $cells_days_term->name ) ?></span>
        <?php endforeach; ?>
    </li>
    <!-- The other <li> elements here -->
</ul>

另一方面,我建议修改第一部分,在你回音的地方使用HTML,所以当一切都说了算后,你的代码会是这样的:

<?php 
$terms = get_terms(\'cell-locations\');
$argv = array(
    \'taxonomy\' => \'cells-days\',
    \'orderby\' => \'by_term\',
    \'hide_empty\' => false
);
$cells_days_terms = get_terms( $argv );
foreach ($terms as $term):
  $wpq = array (\'taxonomy\'=>\'cell-locations\',\'term\'=>$term->slug);
  $myquery = new WP_Query ($wpq);
  $article_count = $myquery->post_count; ?>
  <div class="accordionButton">
    <h2 class="cellHeader" id="<?= $term->slug ?>">
        <?= $term->name ?>
    </h2>
  </div>
  <div class="accordionContent">
    <?php if ($article_count): ?>
      <ul class="cell_list">
        <?php while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
          <li class="cell-item">
            <ul class="cell-list">
              <li>
                <?php $i_max = count( $cells_days_terms ); $i = 0; foreach ( $cells_days_terms as $cells_days_term ): $i++; ?>
                  <span class="cells-days"><?php echo ( $i < $i_max ? $cells_days_term->name . \', \' : $cells_days_term->name ); ?></span>
                <?php endforeach; ?>
              </li>
              <li><?php echo get_post_meta(get_the_ID(), \'_cell_leader\', true); ?> / <?php echo get_post_meta(get_the_ID(), \'_cell_apprentice\', true)?></li>
              <li>Get in touch with <a href="mailto:<?php echo get_post_meta(get_the_ID(), \'_cell_leader_email\', true); ?>"><?php echo get_post_meta(get_the_ID(), \'_cell_leader\', true); ?></a></li>
            </ul>
          </li>
        <?php endwhile; ?>
      </ul>
    <?php endif; ?>
  </div>
<?php endforeach; ?>

结束

相关推荐

使用多个分类对自定义帖子进行排序 - 小码农CODE - 行之有效找到问题解决它

使用多个分类对自定义帖子进行排序

时间:2012-10-27 作者:jsumnersmith

我有一个帖子类型,包括(1)位置和(2)一周中的一天的分类。

我使用第一种分类法将帖子分为多个组。下面是我使用的循环:

<?php 
$terms = get_terms(\'cell-locations\');
$argv = array(
                \'orderby\'       =>  \'by_term\',
                \'hide_empty\'    => false
                );
foreach ($terms as $term) {
  $wpq = array (\'taxonomy\'=>\'cell-locations\',\'term\'=>$term->slug);
  $myquery = new WP_Query ($wpq);
  $article_count = $myquery->post_count;
  echo \'<div class="accordionButton">\';
  echo "<h2 class=\\"cellHeader\\" id=\\"".$term->slug."\\">";
  echo $term->name;
  echo "</h2>";
  echo \'</div>\';
  echo \'<div class="accordionContent">\';
  if ($article_count) {
     echo "<ul class=\'cell_list\'>";
     while ($myquery->have_posts()) : $myquery->the_post();?>
                            <li class="cell-item">
                                <ul class="cell-list">
                                    <li><?php $terms_as_text = get_the_term_list( $post->ID, \'cell-days\', \'\', \', \', \'\' ) ;
                                        echo strip_tags($terms_as_text);
                                    ?> </li>
                                    <li> <? echo get_post_meta(get_the_ID(), \'_cell_leader\', true); ?> / <?php echo get_post_meta(get_the_ID(), \'_cell_apprentice\', true)?></li>
                                    <li>Get in touch with <a href="mailto:<?php echo get_post_meta(get_the_ID(), \'_cell_leader_email\', true);?>"><?php echo get_post_meta(get_the_ID(), \'_cell_leader\', true);?></a></li>
                                </ul>
                            </li>

     <?php endwhile;
     echo "</ul>";
  }
  echo \'</div>\';
}
?>
这为我提供了一个基于分类术语“细胞位置”的手风琴式布局这一切都很好,只是我现在想根据其他分类法“cells days”对每个位置内的帖子进行排序我用插件给他们排序(http://wordpress.org/extend/plugins/taxonomy-terms-order/).插件的api提供以下查询参数来按顺序调用帖子:

 $argv = array(
                \'orderby\'       =>  \'term_order\',
                \'hide_empty\'    => false
                );
get_terms(\'category\', $argv);
在第一个循环中创建第二个循环时遇到问题。有什么想法或建议吗?

1 个回复
SO网友:A.J. Bale

您的部分问题可能是因为您使用的是get_terms() 作用根据the docs for this function:

从4.5.0开始,分类法应该通过$args数组中的\'taxonomy\'参数传递:

$terms = get_terms( array(
    \'taxonomy\' => \'post_tag\',
    \'hide_empty\' => false,
) );
因此,我们可以重写您的get_terms()$argv 对于cells-days 分类法如下:

$argv = array(
    \'taxonomy\' => \'cells-days\',
    \'orderby\' => \'by_term\',
    \'hide_empty\' => false
);
$cells_days_terms = get_terms( $argv );
然后您可以创建另一个foreach 在第一个<li> 中的元素ul.cell-list (至少这似乎是您试图循环通过的地方cells-days):

<ul class="cell-list">
    <li>
        <?php $i_max = count( $cells_days_terms ); $i = 0; foreach ( $cells_days_terms as $cells_days_term ): $i++; ?>
            <span class="cells-days"><?= ( $i < $i_max ? $cells_days_term->name . \', \' : $cells_days_term->name ) ?></span>
        <?php endforeach; ?>
    </li>
    <!-- The other <li> elements here -->
</ul>

另一方面,我建议修改第一部分,在你回音的地方使用HTML,所以当一切都说了算后,你的代码会是这样的:

<?php 
$terms = get_terms(\'cell-locations\');
$argv = array(
    \'taxonomy\' => \'cells-days\',
    \'orderby\' => \'by_term\',
    \'hide_empty\' => false
);
$cells_days_terms = get_terms( $argv );
foreach ($terms as $term):
  $wpq = array (\'taxonomy\'=>\'cell-locations\',\'term\'=>$term->slug);
  $myquery = new WP_Query ($wpq);
  $article_count = $myquery->post_count; ?>
  <div class="accordionButton">
    <h2 class="cellHeader" id="<?= $term->slug ?>">
        <?= $term->name ?>
    </h2>
  </div>
  <div class="accordionContent">
    <?php if ($article_count): ?>
      <ul class="cell_list">
        <?php while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
          <li class="cell-item">
            <ul class="cell-list">
              <li>
                <?php $i_max = count( $cells_days_terms ); $i = 0; foreach ( $cells_days_terms as $cells_days_term ): $i++; ?>
                  <span class="cells-days"><?php echo ( $i < $i_max ? $cells_days_term->name . \', \' : $cells_days_term->name ); ?></span>
                <?php endforeach; ?>
              </li>
              <li><?php echo get_post_meta(get_the_ID(), \'_cell_leader\', true); ?> / <?php echo get_post_meta(get_the_ID(), \'_cell_apprentice\', true)?></li>
              <li>Get in touch with <a href="mailto:<?php echo get_post_meta(get_the_ID(), \'_cell_leader_email\', true); ?>"><?php echo get_post_meta(get_the_ID(), \'_cell_leader\', true); ?></a></li>
            </ul>
          </li>
        <?php endwhile; ?>
      </ul>
    <?php endif; ?>
  </div>
<?php endforeach; ?>

相关推荐