In my last post on how to embed videos in sidebars, I ended with this little teaser:
Did you notice that the video we used as an example is in the sidebar of this post? Scroll up and look on the right hand side. Now check this. Surf to the home page or any other post. POOF! The video is gone! Where did it go? HOW did I do that?
Not tellin!
Ok ok … I’ll tell you all about that in a post soon!
So, here we are. How was it done? The answer is conditional logic!
It’s really not that hard either.
How to do it
Step 1
Go to Plugins / Add New and search for a plugin called ‘Widget Logic’ by Alan Trewartha.
Install the plugin and activate it.
Step 2
Write a post. While you are working on your post, take note of the Post ID. Just look in the URL bar while you are working on your post. For example, this post has a URL that looks like this while I am editing it.
The bit that says post=4193 is the bit we are interested in. Make note of that number in the post you are creating.
Step 3
Lets suppose that your post is all about energy alternatives and you wanted to show a video in the sidebar rather than in the post. But, you only want the video to show up when this particular post is being read.
Follow all the steps in “How To Insert Videos In Sidebars Or Your Home Page“.
This time, when you are pasting the video code into the text widget, because you have activated Widget Logic, you will see a little box at the bottom of your text widget.
This box allows me to apply conditional logic (conditional tag). In this case, the logic is:
If the “post” with the ID of 4193 is being read, show the video. If it is not being read, do not show the video. The syntax is VERY important to get right. In this case, the logic is: is_single( ‘4193’ )
Save your widget and surf to your post. If you did it correctly, your video will be there. Surf away from that post and the video should be gone.
Scroll up and look in the sidebar of this post. You will see a video called Converting Plastic Into Oil. If you go to another post, that video will not be there.
Note: This works great for a few videos. But, if you wanted to have a specific video for 100 different posts, you could do it this way but you would need 100 text widgets in your sidebar which would become unwieldy to manage! A more elegant solution would need to be found, or, simply embed the videos in the posts.
Taking It A Step Further
In this example, I used a video to illustrate the power of Widget Logic. There are many other ways to use this.
For example:
- You could show a Buy Now button for a specific post, page or category.
- You could show a map to your shop or studio on your about page.
- You could show a link menu to other resources.
The possibilities are endless. All you need to do is place the right code (conditional tag) in the Widget logic box. You can find an exhaustive list of conditional tags here:
I hope that this gives you a good start on how to use conditional tags / conditional logic.
Have fun!
Photo Credit
Thumbnail of Mr. Spock is Wikipedia – Public Domain
Shannon Grissom says
Very cool!
Gil Namur says
Hi Shannon,
I used this quite a bit at your site 😉