【问题标题】:get sub_field from children pages and sort them alphabetically从子页面获取 sub_field 并按字母顺序排序
【发布时间】:2014-04-30 11:39:08
【问题描述】:

我正在使用 php 代码显示来自特定父页面 ID 的子页面,以在选择列表类型中显示页面链接。

我所有的子页面都有一个名为“artiste”的子字段,带有一个艺术家姓名。

这是我的 php 代码,用于显示带有页面链接的页面名称:

<?php

        $args = array(
        'post_type'      => 'page',
        'posts_per_page' => -1,
        'post_parent'    => '22',
        'order'          => 'ASC',
        'orderby'        => 'title'
        );

        $parent = new WP_Query( $args );

        if ( $parent->have_posts() ) : ?>

        <div class="styled-select">

            <select name="" onchange="location = this.options[this.selectedIndex].value;">

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



                    <option value="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></option>



            <?php endwhile; ?>

            </select>

        </div>

        <?php endif; wp_reset_query(); ?>

这很好用,我得到所有子页面标题和永久链接并按字母顺序排序。

现在我想从每个子页面中获取一个自定义字段而不是页面标题,这是我的代码: 我的自定义字段是“the_sub_field('nom_artiste');”

        $args = array(
        'post_type'      => 'page',
        'posts_per_page' => -1,
        'post_parent'    => '22',
        'order'          => 'ASC',
        'orderby'        => 'title'
        );

        $parent = new WP_Query( $args );

        if ( $parent->have_posts() ) : ?>

        <div class="styled-select">

            <select name="" onchange="location = this.options[this.selectedIndex].value;">

            <option>--- A - Z ---</option>



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



                    <option value="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php if(get_field('artiste')): while (has_sub_field('artiste') ): ?><?php the_sub_field('nom_artiste'); ?><?php endwhile; endif; ?></option>



            <?php endwhile; ?>


            </select>

        </div>

        <?php endif; wp_reset_query(); ?>

它也可以,我得到的是自定义字段而不是页面标题,以及我的页面标题的永久链接,这很完美。

但在第二个代码中,我的自定义字段是根据页面标题排序的。

例如,我有 2 页:

AAAAA

ZZZZZ

和自定义字段:

对于 AAAA :自定义字段为 GGGG

对于 ZZZZ :自定义字段为 BBBB

所以对于第二个 php 代码,我的列表没有正确排序,因为它根据页面标题而不是自定义字段进行排序...我得到:

GGGG

BBBB

我想我必须用我的自定义字段创建一个数组,以便能够在我的 select onchange 中显示它们之前按字母顺序对它们进行排序,但我不知道该怎么做......

谁能帮我解决这个问题?

非常感谢您的帮助

【问题讨论】:

    标签: php wordpress sorting html-lists alphabetical


    【解决方案1】:

    试试这个,替换代码中&lt;?php while ... endwhile; ?&gt; 之间的所有内容:

    <?php
       //Declare an array to hold data
       $options = array();
    
       //The loop
       while ( $parent->have_posts() ) : $parent->the_post();
           //Define a temporary array
           $option = array();
    
           //Get all the values we need and store them in an array.
           $option['value'] = get_permalink();
           $option['title'] = get_the_title();
           if(get_field('artiste')): 
               while (has_sub_field('artiste') ) {
                  $option['label'] = get_sub_field('nom_artiste'); 
               }
           else: 
               $option['label'] = get_the_title(); //Put something here if artiste is not populated.
           endif;
           $options[] = $option;
        endwhile;
    
        //Sort the array on the 'label' field
        usort($options, function($a, $b) { return strcmp($a['label'], $b['label']); });
    
        //Output the <option> tags
        $format = '          <option value="%s" title="%s">%s</option>'."\n";
        foreach ($options as $o) {
             printf($format, $o['value'], $o['title'], $o['label']);
        }
    ?>  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-12
      • 2020-11-26
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多