【问题标题】:Order Posts by Custom Field Value and display the value once按自定义字段值排序帖子并显示一次值
【发布时间】:2014-03-17 13:16:19
【问题描述】:

我想通过自定义字段值显示 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
  • 酒店3

城市 2:

  • 4号酒店
  • 5号酒店

城市 3:

  • 酒店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; ?>

【问题讨论】:

    标签: php wordpress post field


    【解决方案1】:

    这是一个基本的伪代码示例,说明如何完成此操作。注意:变量是组成的,因为我不确定你的变量值是什么。

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

    【讨论】:

    • 感谢您的快速回复!如果用代码更新我的帖子(我是 StackOverflow 的新手,不知道这是否是正确的方法),我已经用它走了多远。它现在在每个条目中显示位置(城市),而不仅仅是一次。我认为那是因为我现在还没有定义$current_row$row_count。但我不明白,我需要用它们做什么。
    • 我更新了答案...省略了我设置 $previousCity 的部分(更改为 $previousLocation 以匹配您的)。 $safari_query 应该有一个获取行数的方法,但我不确定它是什么,不知道查询是如何构建的。
    • 现在开始工作了!感谢那! (我在第一篇文章中粘贴了我的工作代码)
    猜你喜欢
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 2014-08-02
    相关资源
    最近更新 更多