- This topic is empty.
-
AuthorPosts
-
October 1, 2014 at 6:32 am #185174
taxicss
ParticipantHi, just a quick question. How do I dynamically add a class of “has_sidebar” to the body if the page has an active sidebar?
Thanks.
October 1, 2014 at 6:43 am #185176chrisburton
ParticipantHave you looked at the Codex?
October 1, 2014 at 6:48 am #185177taxicss
ParticipantI did but what I want is just a class so I can style the pages with sidebars and pages without.
I’ll keep digging. Thanks.
EDIT:
Ok, found it.October 1, 2014 at 6:51 am #185179chrisburton
ParticipantI did but what I want is just a class so I can style the pages with sidebars and pages without.
Yeah. The WP Codex will help you with knowing what to use
I found this within seconds: http://codex.wordpress.org/Function_Reference/is_active_sidebar
October 1, 2014 at 7:11 am #185181taxicss
ParticipantNow I’m confused. All my pages now have active sidebar since I’m using the page.php. How can I activate sidebar to some specific page only?
October 1, 2014 at 7:15 am #185183chrisburton
ParticipantI’m not understanding, can you show me code? Elaborate more rather than a sentence or two if you can.
Edit:
How can I activate sidebar to some specific page only?
Custom Post Types?
October 1, 2014 at 7:23 am #185185taxicss
ParticipantOk, here’s some of my code…
I have 6 pages. They are just normal pages, no templates. Under page.php, I have “<?php get_sidebar(); ?>” which activates the sidebar to every page that is being created.
Now I only want 4 of my pages to have the sidebar?October 1, 2014 at 7:34 am #185190taxicss
ParticipantI’m thinking of the conditional tags to target the specific pages like
if ( is_page( '' ) ) {
...
}
<code></code>but I don’t know what code to put inside to add the sidebar…
October 1, 2014 at 7:35 am #185191chrisburton
ParticipantI have 6 pages. They are just normal pages, no templates. Under page.php, I have “<?php get_sidebar(); ?>” which activates the sidebar to every page that is being created. Now I only want 4 of my pages to have the sidebar?
If I were using WordPress, I would honestly just use a CPT with the same setup as
page.php
except have the sidebar in the CPT template. That would solve your issue.October 1, 2014 at 7:37 am #185192chrisburton
Participantbut I don’t know what code to put inside to add the sidebar…
is_page(array('about', 'work', 'contact')) { sidebar stuff here }
October 1, 2014 at 7:46 am #185194taxicss
ParticipantAh you mean putting this stuff
// If Dynamic Sidebar Exists
if (function_exists('register_sidebar')) {
// Define Sidebar Widget Area 1
register_sidebar(array(
'name' => __('Widget Area 1', 'spiceksa'),
'description' => __('Description for this widget-area...', 'spiceksa'),
'id' => 'widget-area-1',
'before_widget' => '<div id="%1$s" class="%2$s">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>'
));
// Define Sidebar Widget Area 2
register_sidebar(array(
'name' => __('Widget Area 2', 'spiceksa'),
'description' => __('Description for this widget-area...', 'spiceksa'),
'id' => 'widget-area-2',
'before_widget' => '<div id="%1$s" class="%2$s">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>'
));
}
<code></code>inside that?
October 1, 2014 at 7:47 am #185195chrisburton
ParticipantEdit:
Oh, I didn’t understand what you were implying. Yes, put it inside of that.
October 1, 2014 at 8:01 am #185196Alen
ParticipantPut this in your
functions.php
file. Note: Not tested./** * ---------------------------------------- * Adds sidebar name, as class, on the body * ---------------------------------------- */ add_filter('body_class', 'init_add_sidebar_classes_to_body'); function init_add_sidebar_classes_to_body($classes = '') { if ( is_active_sidebar( 'some-sidebar' ) ) { $classes[] = 'some-sidebar'; } return $classes; }
October 1, 2014 at 8:04 am #185197chrisburton
Participant@AlenAbdula Thanks for chiming in! Seemed like the OP wanted two things: add class to the body, only have the sidebar appear on specific pages.
October 1, 2014 at 8:05 am #185198 -
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.