0

How would I add a class coming from a custom field to a widget? I know I should put it somewhere into $before_widget ... in my case this is

'before_widget' => '<aside id="%1$s" class="widget %2$s grid-33'.$color.'">',

I tried using $color and adding

global $post;
$color = get_post_meta($post->post_parent, 'bg_color', true );

above, but obviously I am not doing it the right way since it doesn't spit out the color class.

  • 1
    So you want to have the option to set different background color for some widgets when viewing single posts? – birgire Jan 22 '14 at 18:29
  • You can not define widget class from postmeta on register widget function. For before_widget argument, you can try to put a placeholder class which you will be replaced on the widget() method on your widget class. Like: 'before_widget' => '<aside id="%1$s" class="widget %2$s I_WILL_REPLACE_YOU_LATER">' , and later on widget method after extracting $arguments, $before_widget = str_replace('I_WILL_REPLACE_YOU_LATER', $color, $before_widget ); . – Shazzad Jan 22 '14 at 20:04
  • Hi Birgire, I want the wordpress editor to be able to change the background color of widgets using a dropdown within in the widget. That's what I did Shazzad, I ended up using a slightly adjusted version of this: ednailor.com/2011/01/24/… – inTOWN Jan 22 '14 at 20:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.