0

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?

1

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 );
  • I should add the $country_flag = get_post_meta( $post->ID, 'country', true ); in single.php after <?php the_title(); ?> to display the custom field after title of the post? – Alin Tanase Sep 27 '14 at 11:07
  • 1
    <?php echo get_post_meta( get_the_ID(), 'country', true ); ?> – Steven Jones Sep 27 '14 at 14:42
  • Very nice!Thanks alot! One final question. If i want to display the flag in the home screen after the views,comment and likes icons like in this image s15.postimg.org/pe9dae4fv/cacat.png? The theme is detube. I just need to put the code in the template file just like I did with the one in the title? – Alin Tanase Sep 27 '14 at 16:43
  • Why don't you try it yourself? Then, if you can't get it working update your code. Also upvote the answer if you found it helpful. – Steven Jones Sep 28 '14 at 7:57

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.