Show a custom field value(s) for a specific key, echo it and link it
-
Hello, may be this is daily bread to a lot of you but I had to struggle to get this right, so I hope it will help some or at least one person out there.
With the following piece of php you will be able to get the values assigned to a specific key, echo it and link it. I use it in my post pages to customize the navigation bar
<?php $value = get_post_meta($post->ID, 'insert your $key here', true); if ($value=="your value here") echo "<a href='https://www.yourlink.com'>Hyperlink Text</a>"; ?>
TRUE returns the first value for your $key, false would return them all.
If you would like to customize more posts depending on the value assigned to a common $key (say post1 and post2 have the same $key but different values for it) you may simply apply an elseif tag like this
<?php $value = get_post_meta($post->ID, 'insert your $key here', true); if ($value=="your value here") echo "<a href='https://www.yourlink.com'>Hyperlink Text</a>"; elseif($value=="your other value here") echo "<a href='https://www.yourlink.com'>Hyperlink Text</a>"; ?>
You may add as many elseif as you need
That’s it…hope it helps
- The topic ‘Show a custom field value(s) for a specific key, echo it and link it’ is closed to new replies.