【发布时间】: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