I need to display images and other content in the sidebar depending on a page. Pages are added dynamically and information that need to appear in the sidebar is different for each page. So I can’t predict what will go where. So the idea was is to have a page’s content as usual (with all the neccessary shortcodes) and then at the end of page content have a shortcode with all the info that needs to appear in the sidebar for that page.
Original question: displaying shortcodes selectively (suppress in the main content and instead display in a sidebar)
I was wondering if it’s possible to display shortcodes “selectively”.
For example, a specific shortcode, let’s call it [sc]
, is specified within a page content. Now, when displaying that page’s content I would like to suppress that specific [sc]
shortcode, but instead display it from the sidebar for that page. Content of a page will have other shortcodes. I only want to do selectively display the specific [sc]
shortcode and process others as usual – i.e. other shortcodes will be processed within the page’s content.
Is that possible to do? And how?
A shortcode handler doesn’t have to return anything, it can just modify a global variable that you later read in your sidebar widget. If your sidebar is displayed after your post content (most likely if it’s a right sidebar), you can do this without any problem. If your sidebar is displayed before the post content it is more complicated: you have to start output buffering when your widget is called, wait until the rest of the page is displayed, and then do something with the shortcode content and flush the output buffer.
A simple “proof of concept” that displays the shortcode content not in the post but in the footer:
I have based my solution on
Jan Fabry's
answer. I don’t have widgets, but simple template files.So, what I did is the following:
Specified all the info that needs to appear in the sidebar within a page’s content itself wrapped in
[sidebar_content]
shortcode.Specified the following function in the functions.php. This expands all the content of
[sidebar_content]
, but doesn’t display anything, but instead saves it in the global variable.In the
sidebar.php
. I have the following code:You can use conditional tags and do_shortcode.
http://codex.wordpress.org/Function_Reference/do_shortcode
http://codex.wordpress.org/Conditional_Tags
A simple example for a single page with an ID of 12.
If instead you wrote this shortcode yourself, you can create a function to give it a display parameter, like
[shortcode('page=5, 6, 7', 'category', 'exclude=posts', 'whatever')]
You could do it based on a custom field, for example create a custom field named
sc_no_show
and give it the value of true.then in your shortcode function do something like this:
Now on every page or post you have a custom fields named
sc_no_show
with the value oftrue
it wont show/You could use a few well placed actions to look ahead for your shortcode, if it’s found set a flag and run additional actions to first strip the content of that shortcode, then a secondary one to execute that shortcode in the sidebar. Alongside all that create a simple action of your own inside the sidebar file and hook onto that when you need to print out the shortcode.
Not following? Don’t blame you, i’m not always good at explaining ideas, so here’s an example in code form..
First, inside your sidebar where you want the shortcode content appear, add something like..
We now have an action we can hook onto when a flag gets set.
Next we need to scan the posts, before the loop occurs, check for the existance of a particular shortcode, i’ll use
[sc]
as per your example and we can do that by hooking ontothe_posts
. Rather than using a global, i prefer to just wrap the code into a class, and define a class variable inside the class to act as the flag.So basically what happens is this..
the_posts
checks each post for the shortcode, if found sets the flag.the_content
executes later, and checks if the flag is set.If you’re referring to your own shortcode that you created, and it’s use in this way..
..then you’ll need something smarter to strip the shortcode contents from the post content.
If it’s not used like that, then what i’ve provided above should do the job just fine.
Hope that helps.. 🙂