Question

I'm new to wordpress and I am trying to understand these custom fields. I read the wordpress codex page refering to custom fields but I have some questions. I take for example my site. I created a custom field named "country_flag". I want this custom field to store for every post a link to a icon displaying a country flag.

This icon i want to place on my site after the post title to show guest in which language the post is.

On the codex page it tells me to place <?php the_meta(); ?> in my template. But if i have more custom fields and I want only one to be shown in my template where i put the <?php the_meta(); ?>, what code should I place?

Was it helpful?

Solution

If you want to display a single custom field (post meta) then you can use:

get_post_meta();

It has three arguments.

  1. The ID of the post
  2. The name of the post meta field (country_flag)
  3. Whether or not the value returned is a single string (in your case it will be);

So the usage might be something like this:

$country_flag = get_post_meta( $post->ID, 'country', true );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top