按字母顺序获取自定义字段值 [英] Get custom field values in alphabetical order
本文介绍了按字母顺序获取自定义字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的函数按发布顺序列出自定义字段的值.例如:
The function below lists the values of a custom field in the order they are posted. example:
- 弗兰克·卡普拉
- 希区柯克(Alfred Hitchcock)
- 伍迪·艾伦
- 伍迪·艾伦
- 弗兰克·卡普拉
- PedroAlmodóvar
我想按字母顺序且不重复的方式获取此列表,并带有指向每个项目<a href="Woody-Allen">Woody Allen</a>
的链接.例如:
I'd like to get this list in alphabetical order and without repetition, with a link to each item <a href="Woody-Allen">Woody Allen</a>
. example:
- 希区柯克(Alfred Hitchcock)
- 弗兰克·卡普拉
- 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; ?>
这可能吗?
推荐答案
使用 get_posts
,您可以像这样用meta_value
订购帖子:
$movie_reviews = get_posts(array(
'numberposts'=>-1,
'order'=>'ASC',
'orderby'=>'meta_value',
'meta_key'=>'director'
));
要删除重复项,您可以构建一系列控制器:
To remove duplicates, you can build an array of directors :
$directors = array();
foreach( $movie_reviews as $post ) {
$director = get_post_meta( $post->ID, 'director', true );
}
$directors = array_unique($directors);
然后您可以根据需要显示它们:
And after you can display them as you want :
foreach ($directors as $director) {
// display what you want
}
编辑:仅显示a *导演:
EDIT : To display only a* directors :
foreach ($directors as $director) {
if (strtolower($director[0])=='a') {
// display what you want
}
}
这篇关于按字母顺序获取自定义字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文