Say you’ve got a single post, page or Custom Post Type (CPT) and you want to display an image with a link to it in a sidebar widget. How do you do it?
If you know HTML then it’s very easy to do it for one image.
- Add a new text widget to the sidebar.
- Use the image tag to display the image: <img>
- Wrap it in an anchor tag for the link: <a>
Using the post ID
<a href="http://www.oik-plugins.com/?p=6436>image tag goes here</a>
OR using the permalink
<a href="http://www.oik-plugins.com/2012/12/show-a-single-image-link-to-a-page-using-bw_thumbs-or-bw_images/">image tag goes here</a>
And that’s fine. Until you want to change the image or the target of the link. BUT wouldn’t it be easier to use a shortcode?
oik shortcode and custom image link URL
Using the oik custom image link URL and a shortcode we can reduce the amount of typing you need to do. The [bw_thumbs] or [bw_images] shortcodes can be used to display attached images and the oik custom image link URL can be used to route the viewer to the target. Here we display the attachment(s) to the page.
Linking directly to the post or page
If you can find a way of narrowing down the query on the post, page or CPT so that only one post, page or CPT is listed, then the attached image will automatically link to the target. This is fairly easy if displaying a page which is a single child of another page. BUT it’s (currently) a bit trickier if there is potentially more than one item listed in the results. [bw_posts post_type=post include=6436 exclude=-1]
[bw_thumbs post_type=post include=6436 exclude=-1]
[bw_images post_type=post include=6436 exclude=-1]
What’s needed is the ability to define the target post ID. AND that’s coming soon, in the next version of the oik base plugin.