按自定义字段值订购帖子并显示一次值

时间:2014-03-17 13:16:20

标签: php wordpress post field

我想按自定义字段值显示wordpress帖子。那并不难。我已经有了:

<?php

// args
$args = array(
  'post_type'   => 'safari',
  'meta_key'  => 'city',
  'orderby'   => 'meta_value',
  'order'     => 'ASC'
);

$safari_query= new WP_Query( $args ); ?>
    <?php if ($safari_query->have_posts()) : ?>

        <?php while ($safari_query->have_posts()) : $safari_query->the_post(); ?>

        // My code here

        <?php endwhile; ?>
        <?php wp_pagenavi(array('before' => '', 'after' => '', 'options' => array(), 'query' => $safari_query)); ?>
    <?php endif; ?>

但我现在需要的是,自定义字段值显示在相关帖子的顶部。像这样:

城市1:

  • 酒店1
  • 酒店2
  • Hotel 3

城市2:

  • Hotel 4
  • Hotel 5

城市3:

  • Hotel 6

等等。

我知道如何将其添加到现有代码中吗?

亲切的问候

编辑:

在Chris的帮助下,我到目前为止得到了它,但它每次都显示城市(现在的位置):

<?php

// args
$args = array(
  'post_type'   => 'resort',
  'meta_key'  => 'wpcf-location',
  'orderby'   => 'meta_value',
  'order'     => 'ASC'
);

$safari_query= new WP_Query( $args ); ?>
<?php if ($safari_query->have_posts()) : ?>

    <?php while ($safari_query->have_posts()) : $safari_query->the_post(); ?>


        <?php
        $location = get_post_meta($post->ID, 'wpcf-location', true);
        $resort = the_title();
        $previousResort = "";

        if($location !== $previousResort ) {
            //output the location name and start a list, if necessary close the prvevious list
            if($current_row !==1) {
                echo "</ul>";
            }
            echo "$location <br> <ul><li>$resort</li>";

         } else {
             // not a changed location, just output the resort
             echo "<li>$resort</li>";
         }
         //finally, if this is the last row, close the list
         if($current_row == $row_count) {
             echo "</ul>";
         }
         ?>

    <?php endwhile; ?>
<?php endif; ?>

最终编辑:

在Chris的帮助下工作了!谢谢!

 <?php

  // args
  $args = array(
    'post_type'   => 'resort',
    'meta_key'  => 'wpcf-location',
    'orderby'   => 'meta_value',
    'order'     => 'ASC'
  );

  $previousLocation = "";

  $safari_query= new WP_Query( $args ); ?>
      <?php if ($safari_query->have_posts()) : ?>

          <?php while ($safari_query->have_posts()) : $safari_query->the_post(); ?>


              <?php
              $location = get_post_meta($post->ID, 'wpcf-location', true);

              if($location !== $previousLocation ) {
                  //output the location name and the first resort.

               } else {
                   // not a changed location, just output the resort

               }

              $previousLocation = $location; ?>
          <?php endwhile; ?>
      <?php endif; ?>

1 个答案:

答案 0 :(得分:0)

这是如何实现此目的的基本伪代码示例。注意:由于我不确定您的变量值是什么,因此组成变量。

//set up a var to track the city from the previous loop
$previousCity = "";       
<?php while ($safari_query->have_posts()) : $safari_query->the_post(); ?>
    if($city !== $previousCity ) {
        //output the city name and start a list, if necessary close the prvevious list
        if($current_row !===1) {
            echo "</ul>";
        }
        echo "$city <br> <ul><li>$Hotel</li>";
     } else {
         // not a changed city, just output the hotel
         echo "<li>$Hotel</li>"; 
     }
     //finally, if this is the last row, close the list
     if($current_row == $row_count) {
         echo "</ul>";
     }
     $previousCity = $city;
<?php endwhile; ?>

编辑:

遗漏了一个重要的部分!在循环结束之前,您必须将位置设置为previousLocation,否则它将永远不会匹配:

    $previousLocation = $location;
<?php endwhile; ?>