我试图从我所有的自定义帖子中获得三个值,用我去过的地方填充谷歌地图。一切都很顺利,除了我有个问题get_the_term_list
. 出于某种原因,它添加了数字"1" 在每个正确返回的值前面。
例如,下面的代码将为“name”输出以下内容:
Key: name, Value: 1
<a rel="tag" href="http://localhost:8888/mntn/mountain/grossglockner">Grossglockner</a>
<br>
问题代码:
<?php
//get custom posts ids as an array
$loop = get_posts(array(
\'post_type\' => \'trips\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'fields\' => \'ids\'
)
);
//loop through each post
foreach($loop as $p){
//get the meta and taxonomy data
$name = get_the_term_list($p, "mountains",true);
$wtr_longitud = get_post_meta($p,"wtr_longitude",true);
$wtr_latitud = get_post_meta($p,"wtr_latitude",true);
//Add to Array
$map_array[] = array ("name" => $name, "latitude" => $wtr_latitud, "longitude" => $wtr_longitud);
}
//Print array
foreach($map_array as $y) {
foreach( $y as $key => $value){
echo "Key: $key, Value: $value <br />";
}
}
?>
最合适的回答,由SO网友:Rachel Carden 整理而成
问题在于get\\u The\\u term\\u list()函数,该函数由以下参数定义:
get_the_term_list( $id = 0, $taxonomy, $before = \'\', $sep = \'\', $after = \'\' )
您正在定义
$before
参数为true,PHP打印为1,这就是为什么它在列表之前打印1。您应该将参数全部删除:
get_the_term_list( $p, \'mountains\' );
或将其替换为字符串:
get_the_term_list( $p, \'mountains\', \'<ul><li>\', \'</li><li>\', \'</li></ul>\' );