Easy Way For Advertisement In WordPress
November 19th, 2008 by Frank • WordPress Hacks • 6 Comments
Since version 2.5 WordPress introduced the shortcode API, more detail about it in our article „WordPress Shortcode API“. Recently I had a task which is surely common, but with the help of shortcode API and custom fields very easy to solve. I had to include some advertisements (in german: Werbung) into some articles.
In each article the editor puts 3 custom fields: bild (image), link, linktext. In the text he can set the shortcode [ad]. This shortcode will be recognized by WordPress with the help of the following function. It will also load specific data to display the ads here.

Easy advertisement with shortcode-API in WordPress
The following function can be used as Plugin or put in functions.php of your theme.
function example_link() {
global $wp_query;
$postID = $wp_query->post->ID;
$mylink = get_post_custom_values('link', $postID);
$mypic = get_post_custom_values('bild', $postID);
$mylinktext = get_post_custom_values('linktext', $postID);
return '<a href="' . $mylinkΎ] . '"><img title="' . $mylinktextΎ] . '" src="' . $mypicΎ] . '" alt="" /></a>';
}
add_shortcode('werbung', 'example_link');
In your content it will display the image with the link and link text. This is just an example and there are many other possibilities to use this. Of course you could get the data for link, image and link text from the other data fields, but a shortcode API makes it easier to include something like this in their post.
Info
- Published in WordPress Hacks
- Tags: Advertisement, WordPress, WordPress Tutorials, WP2.5
- Comment feed | Trackback URL
- read: 7441 | today: 2
- leave a Comment



Great and easy!!!
Thank you very much. Nice n easy