0

I'm trying to get three values from all my custom posts, to populate a Google Map with the places where I've been. All is going fairly well, except for an issue I'm having with get_the_term_list. For some reason it adds the number "1" in front of each correctly returned value.

As an example, my below code will output the following for "name":

Key: name, Value: 1
<a rel="tag" href="http://localhost:8888/mntn/mountain/grossglockner">Grossglockner</a>
<br>

The problem code:

<?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 />";
                    }
            }
?>
1
  • For now I used trim(); to remove it, but obviously I'd prefer to know why this got added.
    – WouterB
    Mar 13 '12 at 13:39
1

The problem lies with the get_the_term_list() function, which is defined with the following arguments:

get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '' )

You're defining the $before argument as true which PHP prints as 1 so that's why it's printing a 1 before the list. You should either remove the argument altogether:

get_the_term_list( $p, 'mountains' );

or replace it with a string:

get_the_term_list( $p, 'mountains', '<ul><li>', '</li><li>', '</li></ul>' );
2
  • Thanks Rachel. I copy pasted it from another place where I'm outputting it (and where it works without the "1"), so I probably didn't dig deep enough.
    – WouterB
    Mar 14 '12 at 8:50
  • No problem. Glad to help! Mar 14 '12 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.