1

I want to show country flags on post cards.My plan is showing flag icons with below code:

<img src="/icons/<?php echo $tagName; ?>.png" />

However I dont know how can I add country tags to Wordpress.You know there are many many country on the world (If I am not wrong there are nearly 200 country based United Nations).Or maybe the country number of I will add as tag will be depend the icon set which I will find.So How can I add country tags automatically with code?

  • Are post cards a post type? Do you have a country taxonomy? – Tom J Nowell Jan 20 at 11:43
  • @TomJNowell custom post type.no taxonomy – Faruk rıza Jan 20 at 11:52
  • Ooh, so these are not tags, they are posts? That's an important piece of information! You should edit your question to talk about country posts, not country tags – Tom J Nowell Jan 20 at 12:19
  • my sorry, it is taxonomy for custom post type. – Faruk rıza Jan 20 at 12:42
1

I'd recommend making use of this API: https://www.countryflags.io/.

You'll also need a list of countries and country codes. Download that here: https://gist.github.com/keeguon/2310008

Then, you can do something like this to register all of the countries to your custom post type's taxonomy. (you only really need to run this code once)

$countries_json = file_get_contents('/countries.json/');

$countries = json_decode($countries_json, true);

foreach ( $countries as $country ) {
    $term = $country->name;
    $taxonomy = YOUR_COUNTRY_TAXONOMY;
    $args = array(slug => $country->country_code);

    wp_insert_term( $term, $taxonomy, $args);
}

Now, all of the country names should be appearing under that taxonomy and you can assign whatever country you want to your post cards. So that when your looping through all your posts and rendering content you can check what country is selected by getting the terms using get_the_terms( int|WP_Post $post, string $taxonomy ).

When you have the terms for a post. You can render the country flag like this:

<img src="/icons/<?php echo $term->code; ?>.png" />

| improve this answer | |
  • I have an error: Invalid argument supplied for foreach() in /home/deniztas/themeforest-deneme2.deniz-tasarim.site/wp-content/themes/html5blank-stable/functions.php on line 28 28.line: foreach ( $countries as $country ) { – Faruk rıza Jan 20 at 21:08
  • Did you download the JSON file and add it to the same directory you ran this code from? – Waldo Rabie Jan 20 at 21:09
  • yes, I repeated exactly what you have said.It is in main theme folder – Faruk rıza Jan 20 at 23:15
  • I changed the code to '$countries_json = file_get_contents('themeforest-deneme2.deniz-tasarim.site/wp-content/themes/html5blank-stable/countries.json');' however it gives no such file or directory error.I can enter the file with this url: themeforest-deneme2.deniz-tasarim.site/wp-content/themes/… – Faruk rıza Jan 24 at 18:33
  • when I changed the url to $countries_json = file_get_contents(get_stylesheet_directory_uri().'/countries.json'); there is only supplied for foreach() in /home/deniztas/themeforest-deneme2.deniz-tasarim.site/wp-content/themes/html5blank-stable/functions.php on line 15 error – Faruk rıza Jan 24 at 20:08

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.