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.

So I have a Country taxonomy with the country names as terms. The country names are represented by clickable flags (png images) on a map. Now I only want to show flags for terms that have countries in them, so what I need is simply to hide the flags representing empty terms.

I'm not a php developer, but here's an excerpt from the map's css:

<div class="mapCont">
<img class="mapAdjust" src="<?php bloginfo('template_url'); ?>/img/map-adjust.gif" />
    <img class="map" src="<?php bloginfo('template_url'); ?>/img/map.jpg" />
        <div class="flagSweden">
            <a href="<?php echo home_url();?>/country/sweden" alt="Sweden" title="Sweden">
            <img class="flag" src="<?php bloginfo('template_url'); ?>/img/flags/sweden-flag.png" /></a> <!-- Sweden -->
        </div>  <!-- /flagCont -->
        <div class="flagChina">
            <a href="<?php echo home_url();?>/country/china" alt="China" title="China">
            <img class="flag" src="<?php bloginfo('template_url'); ?>/img/flags/china-flag.png" /></a> <!-- China -->
        </div>  <!-- /flagCont -->
        <div class="flagBrazil">
            <a href="<?php echo home_url();?>/country/brazil" alt="Brazil" title="Brazil">
            <img class="flag" src="<?php bloginfo('template_url'); ?>/img/flags/brazil-flag.png" /></a> <!-- Brazil -->

etc...

Any ideas? Thank you!

share|improve this question
    
generate your flags code dynamically by using get_terms(); function –  Anjum Sep 15 '13 at 19:37
add comment

1 Answer

up vote 1 down vote accepted

Instead of write manually the html code for your flags, you should create it using php and wordpress functions:

$countries = get_terms('country', array('hide_empty' => true) );
$exclude_terms = array('featured');

if ( ! empty($countries) && is_array($countries) ) { ?>

  <img class="mapAdjust" src="<?php bloginfo('template_url'); ?>/img/map-adjust.gif" />
  <img class="map" src="<?php bloginfo('template_url'); ?>/img/map.jpg" />

  <?php
    foreach ( $countries as $country ) {
      if ( ! empty($exclude_terms) && in_array($country->slug, $exclude_terms) ) continue;
      $name = esc_attr($country->name);
      $slug = $country->slug;
  ?>

    <div class="flag<?php echo str_replace(' ', '', $name); ?>">
      <a href="<?php echo get_term_link($country, 'country') ?>" alt="<?php echo $name; ?>" title="<?php echo $name; ?>">
        <img class="flag" src="<?php bloginfo('template_url'); ?>/img/flags/<?php echo $slug; ?>-flag.png" />
      </a> <!-- <?php echo $name; ?> -->
    </div>  <!-- /flagCont -->

  <?php
  }
}

I've used only 2 functions:

share|improve this answer
    
You genious! I more or less copied your code as is and it just worked. Thanks a lot my friend. Two small questions though: One term is not a country represented on the map, but a Featured term. How would I go about excluding that from the code? Also, as of now all flags link to the term page I'm currently visiting. How can I fix that? Thanks again! –  Exclusif Sep 16 '13 at 10:02
    
One more thing. Some countries, like South Africa, consists of two words, which means the flag tries to get the class "flagSouth Africa". Is there a way to remove the space in case there is one? –  Exclusif Sep 16 '13 at 10:23
    
(1) The featured term is from the same taxonomy. Its used to feature certain posts from all country terms. Some posts can and will belong to both the featured term and a country term. Since the featured term is not a country it shouldn't show up on the map though. (2) I want the flags to link to the respective country term. As of now if I'm on lets say the Brazilian term page, then all the flags are linking to the Brazilian term, not just the Brazil flag. (3) I used str_replace and it worked like a charm! Thanks a lot for your help. –  Exclusif Sep 16 '13 at 17:40
    
@Exclusif (1) I've add the feature of exclude one or more term. in the array $exclude_terms = array('featured'); put all the terms slug you want to exclude. I've used 'featured' because I dont kno which is the sluf of your feature term. (2) That was just a littele bug, I forgot an echo on the code, now fixed. --- Try now the new code, and if it works for you, please, mark the answer as accepted. –  G. M. Sep 16 '13 at 22:38
    
It worked wonders. Everything is flawless now. Thanks once again friend. –  Exclusif Sep 17 '13 at 7:48
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.