The question is pretty simple, but it's complex in fact.

I have implemented custom sidebars for each post/page function in my blog.

It looks like:

<div id="<?php echo get_option('sidebar-layout');" ?>
 <div id="content">
   Some content
 <div id="sidebar">

sidebar-layout option can have three values - left, right and none. And none is default. They are all set via metabox under post/page.

The biggest problem here is that when user chooses left - the first div gets "left" class and everything else is based on this ID, eg.

#left #content {

And when he takes "right" then different style is applied:

#right #content {
     another style;

So my "myshortcode" should basically change parents DIV id.

Any ideas how to achieve that? I believe my shortcode will be executed AFTER the div ID.

  • 1
    Can you explain the link between your shortcode and your sidebar-layout setting? The way you describe it, it looks like you use post metadata. Your code example uses get_option() which reads site-wide options.
    – Jan Fabry
    Commented Apr 6, 2011 at 12:34

1 Answer 1


Use this code to echo your div's ID

<?php $sidebar_layout = get_post_meta($post->ID, 'sidebar-layout', $single = true); ?>
<div id="<?php echo $sidebar_layout ?>">
    <div class="content">
    <div class="sidebar">

If you want to apply styles according to the selection the user has made then change your style sheet as follows:

#left .content{ ... }
#left .sidebar{ ... }
#right .content{ ... }
#right .sidebar{ ... }
#none .content{ ... }
#none .sidebar{ ... }

I hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.