简体   繁体   中英

List all posts in custom post type by taxonomy in 2 columns

I'm trying to get all custom posts by their taxonomy and then put the first two tax in one column and the rest in the second column. But my code loops everything and I get multiple divs. Ideas?

Here's my code :

<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

No way, so I use multiple query like this...

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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