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.

Continue Reading


  1. Pingback: WordPress Links - Week 2 2009 | - 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

Tips & Tricks

Pulling custom fields from outside the loop



In the last post “Creating a custom widget” I showed you how to create a custom widget. Well in this post I will show you how I used my custom widget to display all post with a certain custom field from outside the WordPress loop.

In the last post I used this tag:

<?php include(TEMPLATEPATH . '/includes/showcase.php'); ?>

Now I will show you what the file showcase.php has:

global $wpdb;

$sql = "SELECT wposts.*
	FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
	WHERE wposts.ID = wpostmeta.post_id
	AND wpostmeta.meta_key = 'gallery-url'
	AND wposts.post_status = 'publish'
	AND wposts.post_type = 'post'
	ORDER BY wposts.post_date DESC LIMIT 8";

$pageposts = $wpdb->get_results($sql, OBJECT);
$output = $pre_HTML;

foreach ($pageposts as $post) : setup_postdata($post);  ?>

<li><a href="<?php echo get_post_meta($post->ID, "gallery-url", $single = true); ?>" title="Link to <?php the_title(); ?>">
<img src="<?php echo get_post_meta($post->ID, "image", $single = true); ?>" alt="<?php the_title(); ?>" /></a></li>

<?php endforeach; ?>


That’s it! The most important item you may want to change for your own custom field is the line : AND wpostmeta.meta_key = 'gallery-url'. Where you would change the text in bold to match your own custom field value.


Check out Austin from PressedWords comment below.

With his great advise I was able to figure out why all my attempts to use the query_post weren’t working. it came down to this line of code: <?php echo get_post_meta($post->ID, "gallery-url", $single = true); ?>, that was what I had in my code, and the $post->ID is the reason my code would not echo or print the custom field’s value. Solution? replace $post->ID with get_the_ID(). HA, so simple.

Continue Reading

Tips & Tricks

Creating a custom widget



Today let’s learn a simple quick trick on how to create a custom widget. For my example I will show you how I created my Showcase widget located in the middle, to the right of the posts.

First under your functions.php file type in the following:

<?php // Custom Widget
function MyCustomWidget() { ?>
<li class="widget">
    <h2 class="heading">Latest Showcase</h2>
        	<?php include(TEMPLATEPATH . '/includes/showcase.php'); ?>

<?php }

register_sidebar_widget('The Custom Widget for Showcase', 'MyCustomWidget'); ?>
  • Always make sure your code is between the <?php and ?> for it to work.
  • Once we call the function, the rest is assuming html code that you may or may not need.
  • For instance, you may just put in a picture and call it a day. But my code starts with <li< because my sidebar’s start and end with <ul<.
  • Any way, once your done, just set the final “register_sidebar_widget('the widget title', 'the name of the function');

That’s it! Now you have a custom widget with what ever you want!

Continue Reading

Tips & Tricks

Display custom url if comment authors url is blank



Here is a nifty trick for your comments.php template. If someone comes to your site and leaves a comment but doesn’t leave a url back to there site, the default link that is shown in place of the php code comment_author_url is the current page link.

That might not look good. So, here is a little trick that I just implemented into my site. Besides installing the twittar plugin and pulling Twitter avatars I wanted to use the image itself for the authors url like. But if the author doesn’t have a Twitter avatar or a Gravatar it will display a default Gravatar that I set. If the default Gravatar is shown, usually the author will not leave a url link.

I would like to link the image to so that they can get an image attached to their email address. So.. here is the code:

<?php if($comment->comment_author_url != "") { ?>
<a href="<?php comment_author_url(); ?>"><?php gravatar(); ?></a>
<?php } else { ?>
<a rel="nofollow" href="" title="Get a Gravatar today!"><?php gravatar(); ?></a>
<?php } ?>
Continue Reading

Random Search Terms


Top Articles

Top Searches

powered by WassUp

Recent Posts: Fully Net Worth . com

Terry Fator net worth.

Terry Fator net worth.

Terry Fator net worth Terry Fator is a famous impressionist, comedian, singer, and ventriloquist from America. He has a net worth of $160 million, which he has accumulated over the years as a performer. He was born in 1965 in Dallas, Texas. He is capable of making more than 100 impressions of the ventriloquial kind […]

Emma Barton Net Worth: How Rich is the English Actress Actually?

Emma Barton Net Worth: How Rich is the English Actress Actually?

Emma Louise Barton is an English actress who is best known for playing the role of the character named Honey Mitchell in EastEnders. She is currently part of the seventeenth edition of the popular dance show called Strictly Come Dancing. As of 2019, Emma Barton net worth is estimated to be $2 million. Barton was […]

NF Net Worth: How Rich is the Rapper in 2019 Actually?

NF Net Worth: How Rich is the Rapper in 2019 Actually?

Nathan John Feuerstein, who is better known by his stage name as NF, is an American rapper and songwriter. NF is the ultimate underdog as he is lesser-known among people yet his works are of the highest quality and have done great. His 2017 album called Perception debuted at No. 1 on the Billboard 200 […]


%d bloggers like this: