Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a long list of taxonomy terms that all look like City-ST-Zip, ie: Phoenix-AZ-85043 or San-Diego-CA-92037

How do I print the list of terms so that the link URL remains intact but the link text is just the city name? ie:

<li><a href="http://mydomain/zip/phoenix-az-85043">Phoenix</a></li>

Also - A lot of cities have multiple zip codes, but I don't want to show a city name more than once.

share|improve this question
    
are you ok with showing just the first occurring zip code link for any duplicate city names? –  Michael Apr 14 '12 at 10:55
    
Yes, that is what I was thinking. –  Carson Apr 14 '12 at 16:40
add comment

2 Answers

<?php 
    $terms = get_terms( $taxonomies, $args );
    $term_name_array = array();
    foreach($terms as $term){
        $term_name_explode = explode("-",$term->term_name);
            $myterm_name='';
            for($i=0;$i<count($term_name_explode)-2;$i++){
               $myterm_name.=" ".$term_name_explode[$i];
            }
            $myterm_name = ltrim($myterm_name);
        if(in_array($myterm_name,$term_name_array))
            continue;
        $term_name_array[] = $myterm_name;
        ?>
            <li><a href="<?php echo get_term_link($term->term_id)?>"><?php echo $myterm_name;?></a></li>
        <?
    }
?>
share|improve this answer
    
this does not take care of unique city names, and the 'explode' solution will be problematic with terms such as the given example 'San-Diego-CA-92037' –  Michael Apr 14 '12 at 10:53
1  
@Michael have edited my answer... –  Rajeev Vyas Apr 14 '12 at 11:15
    
I am not able to get this code to work. Is there a variable that I need to replace with the actual name of the taxonomy "zip-code"? –  Carson Apr 14 '12 at 16:49
    
in the code, have you specified your taxonomy for: codex.wordpress.org/Function_Reference/get_terms –  Michael Apr 14 '12 at 17:19
    
When i use Rajeev's answer all that is returned is one bullet point from a list with no text. –  Carson Apr 14 '12 at 19:41
add comment

I hate to answer my own question, but after spending all day on this, I think I found a better approach (or at least a working approach). I am sure that is a cleaner way to do the pre_replace and I have not solved the duplicate term problem, so I will leave this question open hoping someone will provide an even better answer.

<?php

$terms = wp_get_post_terms($post->ID, 'zip-code', array("fields" => "all"));
foreach ($terms as $term) {
    $name = $term->name;
    $name = str_replace("-", " ", $name);
    $name = preg_replace('/\b[A-Z]{2}\b /', '', $name);
    $name = preg_replace("/[0-9]/", "", $name);
        echo '<li><a href="'.get_term_link($term->slug, 'zip-code').'" title="">'.$name.'</a></li>';
}

?> 
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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