按字母顺序获取自定义字段值

时间:2012-09-21 05:11:39

标签: wordpress list field

下面的函数按照发布顺序列出自定义字段的值。例如:

  • Frank Capra
  • Alfred Hitchcock
  • 伍迪艾伦
  • 伍迪艾伦
  • Frank Capra
  • PedroAlmodóvar

我希望按字母顺序获取此列表,不重复,并指向每个项<a href="Woody-Allen">Woody Allen</a>的链接。例如:

  • Alfred Hitchcock
  • Frank Capra
  • PedroAlmodóvar
  • 伍迪艾伦

这是代码:

<?php
$movie_reviews = get_posts( 'numberposts=-1&orderby=post_name' );
foreach( $movie_reviews as $post ) : setup_postdata( $post );
?>
<span>
<?php $director = get_post_meta( $post->ID, "director", $single = true );
if( $director !== '' ) {
echo $director;
} ?>
</span>
<?php endforeach; ?>

这可能吗?

1 个答案:

答案 0 :(得分:0)

使用get_posts,您可以订购meta_value这样的帖子:

$movie_reviews = get_posts(array(
  'numberposts'=>-1,
  'order'=>'ASC',
  'orderby'=>'meta_value',
  'meta_key'=>'director'
));

要删除重复项,您可以构建一系列导演:

$directors = array();
foreach( $movie_reviews as $post ) {
    $director = get_post_meta( $post->ID, 'director', true );
}
$directors = array_unique($directors);

然后您可以根据需要显示它们:

foreach ($directors as $director) {
    // display what you want
}

编辑:仅显示*导演:

foreach ($directors as $director) {
    if (strtolower($director[0])=='a') {
        // display what you want
    }
}