Subscribe via

Use Custom Fields to Dynamically Display Content

You might have noticed that I have a post footer section on every one of my posts. What you might not have noticed is that this post footer changes depending on what type of post it is. For example on each of my plugin pages (e.g. SEO No Duplicate plugin), you will see that the footer looks like this:
Plugins Footer
But on each of my popular articles pages (e.g. Make Any Plugin Work with WP Super Cache), you will see that my footer looks like the following:
Articles Footer
Better yet, on any other posts, the footer shows:
Other Footer

In this post I will show you how to easily implement this feature on your WordPress blog.


You will be doing the following:

  1. Modifying your theme to recognize a post’s type by looking at the post’s custom fields for a particular key.
  2. Setting post types for special posts (e.g. plugin pages, popular articles).


You will need to add the following into your functions.php

function get_custom_field_value($key, $print = false) {
  global $post;
  $value = get_post_meta($post->ID, $key, true);
  if ( $print == false ) return $value; else echo $value;

When called, this function returns the current post’s custom field value.

Modify your theme

You can add the following code template anywhere in the WordPress loop that you desire this functionality (single.php within “while (have_posts())” works for me).

  // Start dynamic section
  if ( function_exists('get_custom_field_value') ) {
    if (get_custom_field_value('plugin_page', false)) {
      // display plugin page stuff here
      echo "Hello World! This is a plugin page!";
    elseif (get_custom_field_value('article_page', false)) {
      // display article page stuff here
      echo "Hello World! This is an article page!";
    else {
      // display default page stuff here
      echo "Hello World! This is an ordinary page!";
  else {
    // display default page stuff here
    echo "Hello World! This is an ordinary page!";

Note the keys ‘article_page’ and ‘plugin_page’, I will be talking about it in the next section.

Setting post type

All you have to do to set the post type is to open up your post within the WordPress admin and add the custom field key that your theme is expecting (‘article_page’ or ‘plugin_page’ in this case).
Now if you visit that post, you should see the text “Hello World! This is an article page!” in the dynamic section. If you visit any other post, you should see the text “Hello World! This is an ordinary page!” instead.

You can now go through all of your special posts and set their type as they should be set.

What’s next?

I realize that there are a lot better ways to do this sort of dynamic content displaying, but I think this is probably the easiest to implement. Looking forward, I want to look into the ability to be able to display such a dynamic message based on whether the post is in a specific category or whether the post has a specific tag.

I would love to hear what you guys think about this technique. Please don’t be shy to critique and let me know if you have a better solution for this.

8 Responses to “Use Custom Fields to Dynamically Display Content”

[go to last comment]
  1. SidSavara

    Ok that's pretty awesome. Now, we just need to put that together with the Subscribe-Remind and/or Similar Posts style Plugin so we can configure it rather than hacking the theme 😉

  2. Terkel Gjervig

    Thanks once again!… Love your tips and tricks, keep up the good work.
    I like the way you make "smart" blogs which generate output – corresponding on different factors.

  3. Ty Bone

    @Terkel Gjervig
    Thank you for your kind words! "Smart" blogs has been my recent theme lately and I'm planning to put more of these tips up. Thanks for coining the term. I will start using this "Smart" blogs term. 🙂

  4. Phoenixheart

    Yet another brilliant idea! I'm going to do the same with my boring blog.
    Thanks Ty 😉

  5. Ty Bone

    Thanks for the compliment, but you're blog is anything BUT boring. I get lots of useful information from there!

  6. Ty Bone

    @Sid Savara
    I've been planning something like that as a plugin. You will definitely be pleasantly surprised if you stay tuned!

  7. Terkel Gjervig

    Nice if you could make such a plugin! maybe a post-footer plugin which also show Related Posts, and share/bookmark links e.g.

    Anyway, what ever you make I'm pretty exited to see the result and of cause add it to my blog 😉

  8. bharathi

    thank you
    …… nice tips………

[go to first comment]

Leave a Reply