Categories: 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.

Web Master

Hi, I am Miguel, I bought this site in 2009. So I now run or manage the site. Please visit my new website or follow me on twitter @W3i.

Recent Posts

WordPress Tips for Photographers

Photography is the art of capturing naturalistic images with the help of photographic cameras. It…

5 hours ago

10 Best Digital Marketing Plugins for WordPress 2022

Have you opened up a new website or a business and want to drive more…

7 hours ago

Configure your WordPress Site for SEO

WordPress has evolved greatly from its origin as a powerful blogging platform to an effective…

19 hours ago

In the yеar 2002, Mіcrоѕoft саmе uр wіth a whоlе new рlаtfоrm for developing арplicаtiоnѕ.

Busіnеsѕеs аnd orgаnіzatіоns аll аrоund the wоrld arе loоking fоr а ѕоlutiоn thаt wоuld prоvе…

1 day ago

The following 5 factors detail the key techniques to influence and improve your ranking on Google.

It is essential to draw people’s attention to your website or blog. It isn’t easy…

2 days ago

Futuristic Cars Make Parking a Piece of Cake

Cars are one of those things that so many of us have that have been…

2 days ago