Php 按自定义字段值排序帖子,并显示一次该值
我想按自定义字段值显示wordpress帖子。这并不难。我已经知道了:Php 按自定义字段值排序帖子,并显示一次该值,php,wordpress,post,field,Php,Wordpress,Post,Field,我想按自定义字段值显示wordpress帖子。这并不难。我已经知道了: <?php // args $args = array( 'post_type' => 'safari', 'meta_key' => 'city', 'orderby' => 'meta_value', 'order' => 'ASC' ); $safari_query= new WP_Query( $args ); ?> <?php
<?php
// args
$args = array(
'post_type' => 'safari',
'meta_key' => 'city',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$safari_query= new WP_Query( $args ); ?>
<?php if ($safari_query->have_posts()) : ?>
<?php while ($safari_query->have_posts()) : $safari_query->the_post(); ?>
// My code here
<?php endwhile; ?>
<?php wp_pagenavi(array('before' => '', 'after' => '', 'options' => array(), 'query' => $safari_query)); ?>
<?php endif; ?>
//我的代码在这里
但我现在需要的是,自定义字段值在相关文章的顶部显示一次。大概是这样的:
城市1:
- 1号酒店
- 2号酒店
- 3号酒店
- 4号酒店
- 5号酒店
- 6号酒店
<?php
// args
$args = array(
'post_type' => 'resort',
'meta_key' => 'wpcf-location',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$safari_query= new WP_Query( $args ); ?>
<?php if ($safari_query->have_posts()) : ?>
<?php while ($safari_query->have_posts()) : $safari_query->the_post(); ?>
<?php
$location = get_post_meta($post->ID, 'wpcf-location', true);
$resort = the_title();
$previousResort = "";
if($location !== $previousResort ) {
//output the location name and start a list, if necessary close the prvevious list
if($current_row !==1) {
echo "</ul>";
}
echo "$location <br> <ul><li>$resort</li>";
} else {
// not a changed location, just output the resort
echo "<li>$resort</li>";
}
//finally, if this is the last row, close the list
if($current_row == $row_count) {
echo "</ul>";
}
?>
<?php endwhile; ?>
<?php endif; ?>
最终编辑:
在克里斯的帮助下成功了!谢谢
<?php
// args
$args = array(
'post_type' => 'resort',
'meta_key' => 'wpcf-location',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$previousLocation = "";
$safari_query= new WP_Query( $args ); ?>
<?php if ($safari_query->have_posts()) : ?>
<?php while ($safari_query->have_posts()) : $safari_query->the_post(); ?>
<?php
$location = get_post_meta($post->ID, 'wpcf-location', true);
if($location !== $previousLocation ) {
//output the location name and the first resort.
} else {
// not a changed location, just output the resort
}
$previousLocation = $location; ?>
<?php endwhile; ?>
<?php endif; ?>
下面是一个基本的伪代码示例,说明如何实现这一点。注意:由于我不确定变量值是什么,所以这些变量都是虚构的
//set up a var to track the city from the previous loop
$previousCity = "";
<?php while ($safari_query->have_posts()) : $safari_query->the_post(); ?>
if($city !== $previousCity ) {
//output the city name and start a list, if necessary close the prvevious list
if($current_row !===1) {
echo "</ul>";
}
echo "$city <br> <ul><li>$Hotel</li>";
} else {
// not a changed city, just output the hotel
echo "<li>$Hotel</li>";
}
//finally, if this is the last row, close the list
if($current_row == $row_count) {
echo "</ul>";
}
$previousCity = $city;
<?php endwhile; ?>
//设置一个var,从上一个循环跟踪城市
$previousCity=“”;
如果($city!=$previousCity){
//输出城市名称并启动列表,如有必要,关闭前一个列表
如果($current_row!==1){
回声“”;
}
echo“$city
- $Hotel
”;
}否则{
//不改变城市,只输出酒店
回声“- $Hotel
”;
}
//最后,如果这是最后一行,请关闭列表
如果($current\u row==$row\u count){
回声“
”;
}
$previousCity=$city;
编辑:
遗漏了一个重要部分!在循环结束之前,必须将位置设置为previousLocation,否则将永远不匹配:
$previousLocation = $location;
<?php endwhile; ?>
$previousLocation=$location;
感谢您的快速回复!如果用代码更新了我的文章(我是StackOverflow的新手,不知道这是不是正确的方法),我已经完成了多少。它现在在每个条目中显示位置(城市),而不是一次。我想那是因为我现在还没有定义$current\u row
和$row\u count
。但我不明白,我需要用它们做什么。我更新了答案。。。省略了我设置$previousCity的部分(更改为$previousLocation以匹配您的)$safari_查询应该有一个获取行数的方法,但是我不知道查询是如何生成的,我不知道它是什么。现在就可以工作了!谢谢你!(我在第一篇文章中粘贴了我的工作代码)