<?php if (get_post_meta($post->ID, \'coach_location\', true) == "Austria"): ?>
。。。这是可行的,但手动检查每个国家并重复帖子内容代码变得越来越困难,是否可以通过国家检查自动显示帖子内容?
因此,不要像这样检查每个国家:
if austria: echo title & the_content
if australia: echo title & the_content
if south_africa: echo title & the_content
... all 196 countries of the world
是否可以自动执行此操作,以便我只回显一次标题和内容?
最合适的回答,由SO网友:1fixdotio 整理而成
如果要按元值对帖子进行分组,请尝试以下代码:
$query = new WP_Query(
array (
\'orderby\' => \'meta_value\',
\'meta_key\' => \'coach_location\',
\'order\' => \'ASC\'
)
);
请参阅
Codex.
假设您要打印每组的Coach位置,示例代码应为:
$last_location = \'\';
while ( $query->have_posts() ) : $query->the_post();
$location = get_post_meta( $post->ID, \'coach_location\', true );
if ( $last_location != $location ){
echo $location;
$last_location = $location;
}
// echo the reset content
endwhile;
wp_reset_postdata();