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.

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

Danny Towers Net Worth: How Rich is the Puerto Rican Rapper?

Danny Towers Net Worth: How Rich is the Puerto Rican Rapper?

Danny Towers is an upcoming rapper who is best known for his songs “‘Motel Hell”, “Geekin”, and “Trauma”. He is currently on tour with Ski Mask The Slump God and was a good friend to late XXXTentacion. As of 2019, Danny Towers net worth is estimated to be $300,000. Danny was born on April 17, […]

Daisy Keech Net Worth: How Rich is the Instagram Star Actually?

Daisy Keech Net Worth: How Rich is the Instagram Star Actually?

Daisy Keech is a social media influencer and model who is famous for being the ‘first certified real booty’. She posts often in bikini with the highlight being her amazing body that many women can only dream about. As of 2020, Daisy Keech net worth is estimated to be $500,000. Keech was born on August […]

Jermaine Whitehead Net Worth: How Rich is the NFL Player Actually?

Jermaine Whitehead Net Worth: How Rich is the NFL Player Actually?

Jermaine Whitehead is an American football player who is currently a free agent. He has played for teams like Baltimore Ravens, Green Bay Packers, and Cleveland Browns. As of 2019, Jermaine Whitehead net worth is estimated to be $1 million. Whitehead was born on March 12, 1993, in Greenwood, Mississippi. He went to Amanga Elzy […]

%d bloggers like this: