Advertisements
Connect with us

Tips & Tricks

Echo custom fields in any category

Here is a neat trick. Say you want to show a custom field in you post or in a certain categories post. There is a simple code you need to write in order to accomplish this:

<?php $image = get_post_meta($post->ID, "image", $single = true); ?>
<?php if($image != '') : if(in_category(7)) { echo ''; } else { ?>
<img src="<?php echo $image ?>" alt="<?php the_title(); ?>" /></a>

In the example above I am calling the variable $image and telling the server that it equals the value of “image” inside get_post_meta or “custom field” of the current post.

Then we are asking if that variable $image doesn’t equal nothing or != and if it is in the category id of 7, echo what ever is in the single quotes (which is nothing in this example), otherwise show the <img> and the value inside “image” which should be the location of the image.

Update: Thanks to Austin from PressedWords for pointing out his trick, from the comment below.

Advertisements
3 Comments

3 Comments

  1. Pingback: WordPress Links - Week 2 2009 | WPStart.org - WordPress themes, plugins and news

  2. Austin

    January 14, 2009 at 12:12 pm

    No need to call get_post_meta() twice. Save some CPU cycles and echo the $image variable instead for the image src attribute.

    Austin’s last blog post..Paged Comments and the SEO Problem: A Solution

  3. frosty

    January 14, 2009 at 2:00 pm

    Thanks for that one, missed it..

You must be logged in to post a comment Login

Random Search Terms

Title

Top Articles

powered by WassUp

Recent Posts: Fully Net Worth . com

LaTisha Scott Net Worth: How Rich is Marsau Scott’s Wife Actually?

LaTisha Scott Net Worth: How Rich is Marsau Scott’s Wife Actually?

LaTisha Scott is a Commercial Real Estate Developer who is the CEO of Infinity Properties. She is best known for starring in the reality television series called Love & Marriage: Huntsville. As of 2019, LaTisha Scott net worth is estimated to be $8 million. She was born on October 1, 1981, in the United States. […]

Myles Stephenson Net Worth: How Rich is the Singer Actually?

Myles Stephenson Net Worth: How Rich is the Singer Actually?

Myles Stephenson is a singer who is best known for winning the popular competition called The X Factor with the boyband Rak-Su back in 2017 in its 14th edition. He is now part of the awaited reality show called I’m a Celebrity…Get Me Out of Here!. As of 2019, Myles Stephenson net worth is estimated […]

Nadine Coyle Net Worth: How Rich is the Singer Actually?

Nadine Coyle Net Worth: How Rich is the Singer Actually?

Nadine Coyle is an Irish singer, actress, and model. She is part of the girl group called Girls Aloud which has been named the United Kingdom’s biggest-selling girl group of the 21st century. As of 2019, Nadine Coyle net worth is estimated to be $13 million. Coyle was born on 15 June 1985, in  Derry, […]

%d bloggers like this: