繁体   English   中英

在2列中按分类列出所有自定义帖子类型的帖子

[英]List all posts in custom post type by taxonomy in 2 columns

我正在尝试按其分类方法获取所有自定义帖子,然后将前两个税项放在一个列中,将其余的第二列中。 但是我的代码会循环所有内容,并且会出现多个div。 有想法吗?

这是我的代码:

<div class="row">
<?php
$post_type = 'myposttype';
$tax = 'mytaxonomy';
$tax_terms = get_terms($tax,'hide_empty=0');

if ($tax_terms) {
  foreach ($tax_terms  as $tax_term) {
    $args=array(
      'post_type' => $post_type,
      "$tax" => $tax_term->slug,
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    ?>

    <div class="column1">
    <?php
    if( $my_query->have_posts() ) {
      if( ($tax_term->name == 'first_taxterm') || ($tax_term->name == 'second_taxterm')  ) {
      echo "<h2 class=\"tax_term-heading\" id=\"".$tax_term->slug."\"> $tax_term->name </h2>";
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
      } else {?>
    </div>
    <div class="column2">
      <?php
      echo "<h2 class=\"tax_term-heading\" id=\"".$tax_term->slug."\"> $tax_term->name </h2>";
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile; }?>
    </div><?php
    }
    wp_reset_query();
  }
}
?>

--

尝试根据$ tax的数量减少wp_query

没办法,所以我这样使用多个查询...

<?php
    $args=array(
      'trend-categories' => 'flowers',
      'post_type' => 'trend',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo '<h2> TITLE BIG </h2><ul>';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <li><a class="hey" href="<?php the_permalink() ?>" rel="bookmark" <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
        <?php
      endwhile;
    }
    wp_reset_query();
    ?>
  </ul>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM