Hot answers tagged

17

Please refer to the get_sidebar() and dynamic_sidebar() Codex entries. The get_sidebar( $slug ) template tag includes the sidebar-$slug.php template-part file. The dynamic_sidebar( $slug ) template tag outputs the $slug dynamic sidebar, as defined by register_sidebar( array( 'id' => $slug ) ).


10

Its the camel-case in the widget ID. The following works: add_action( 'widgets_init', 'ditto_register_sidebars' ); function ditto_register_sidebars() { register_sidebar(array( 'name' => __( 'Right Hand Sidebar' ), 'id' => 'right-bar', 'description' => __( 'Widgets in this area will be shown on the right-hand side.' ) ...


9

You just need to use the alternate syntax for foreach. From the php manual: The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes: foreach (...


9

Widgets DO appear in the Customizer, but only if you are on the page that they are used on. For example, I have a sidebar for the Archive page. When I'm in the Customizer and on the homepage, that sidebar doesn't appear. But if I navigate to an archive page while in the Customizer, it then appears in the widget area.


7

As of at least 3.6, you can use the following code to modify the query used: add_filter('widget_posts_args', 'widget_posts_args_add_custom_type'); function widget_posts_args_add_custom_type($params) { $params['post_type'] = array('post','custom_type'); return $params; } Just add the types you want in the array for post_type and they should appear. ...


7

Defining new sidebar with in your functions.php <?php if ( function_exists('register_sidebar') ) { register_sidebar(array( 'before_widget' => '<li id="%1$s" class="widget %2$s">', 'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>' )); }?> Once ...


6

Your best option is to get a random post author which is a user ID so here is a function to do just that: function get_random_author_wpa91320(){ global $wpdb; $id = $wpdb->get_var(" SELECT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY RAND() LIMIT 1 ...


6

The cart widget isn't showing because it's configured to not show on the cart and checkout page. If you want to change that take a look at class-wc-widget-cart.php, there you find the following line: if ( is_cart() || is_checkout() ) return; Change it to: if ( is_cart() ) return; To show the widget on the checkout page. Note: This will be, if done ...


6

Update: 12/19/15 : Here's a plugin on Github that I developed (using the method in answer I provided below) adding support for changing all widget to bootstrap components/styling. Original Answer I understand not wanting to use javascript, but it seems like overkill in my opinion to completely create a new widget just for the list-group class to be added ...


5

get_stylesheet_directory_uri() returns a value, it doesn’t print anything. So you have to use: echo get_stylesheet_directory_uri(); get_template_part() is just a wrapper for locate_template(). But the latter has one advantage: It returns the path of the file it has found. Try the following: $path = locate_template( 'sidebar-front.php', TRUE ); echo $path;


5

You can do it using Widget Context plugin or Widget Logic plugin. They work fine. I prefer Widget Context because it has more flexibility.


5

If you were to add var_dump($params); to the top of your callback you'd notice that it contains references to both the sidebar name and the sidebar ID. You can use those to control when this runs, provided you know the name or ID. function widget_params( $params ) { if ('Main Sidebar' === $params[0]['name']) { // do something } return $params; } ...


5

The call actually comes from themes compat like you can read here in source. So I'd imagine that you simply copy pasted the complete contents and therefore just added the _deprecated_file() call as well. :D Lesson to learn from this: Never blindly copy paste stuff without understanding what it does and why it does it.


5

Create a function that register sidebars, using register_sidebar, starting from an option: add_action('widgets_init', 'my_custom_sidebars'); function my_custom_sidebars() { $sidebars = get_option('my_theme_sidebars'); // get all the sidebars names if ( ! empty($sidebars) ) { // add a sidebar for every sidebar name foreach ( $sidebars as $...


5

You could add this function to your functions.php file. add_filter( 'sidebars_widgets', 'disable_all_widgets' ); function disable_all_widgets( $sidebars_widgets ) { $sidebars_widgets = array( false ); return $sidebars_widgets; } You could also use the Wordpress conditional tags to disable widgets only on certain pages. For example; this would only ...


5

You could dynamically register sidebars. When you register sidebars, you could query for all of your animal pages, loop over all pages, and register a unique sidebar for each page. Adding a page would automatically create a sidebar for it. However - that's a lot of sidebars. For something tightly coupled with the page content, I would rather handle it with ...


5

The issue is in your params array. There was a missing closing > in before_widget and before-title should be before_title. Try this: array( 'name' => 'Sidebar', 'id' => 'sidebar-1', 'class' => 'custom', 'description' => 'Standard Sidebar', 'before_widget' => '<aside id="%1$s" class="widget %2$s">', '...


4

It sounds like you do want a custom post type, you just want to set the public argument to false. The register_post_type() has lots of arguments that will let you do everything you probably want. Along with public, you'll probably set has_archive to false. Setting show_ui to true will allow you to manage the posts in the backend. If you want to group the "...


4

I guess you could take a big step using wp_list_categories() with a walker to add a additional Unordered List to every item. The code: $cat_id = get_query_var( 'cat' ); $subcats = get_categories( 'child_of=' . $cat_id ); // child categories class Cat_Walker extends Walker_Category { function end_el( &$output, $page, $depth = 0, $args = array() ) { ...


4

Use Conditional Tags to show content only if a certain condition is met. In your case, you're probably looking to use is_front_page(). <aside> <ul> <?php if ( function_exists( 'dynamic_sidebar' ) ) { if ( is_front_page() ) { if ( ! dynamic_sidebar( 'frontpage-widget-area' ) ) { ...


4

As alluded to by @Michael in the comments, the CSS classes for widgets depend on the sidebar they're in more than the widget themselves. Those widget-specific CSS classes can be useful, but not when you're trying to style every widget. It sounds like you may be able to use a normal element selector that targets anything in your sidebar, though I'm unclear ...


4

The CSS classes are applied in the function dynamic_sidebar(). There is no specific filter for that: // Substitute HTML id and class attributes into before_widget $classname_ = ''; foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) { if ( is_string($cn) ) $classname_ .= '_' . $cn; elseif ( is_object($cn) ) $...


4

Look in the parent theme's functions.js. It looks like an implementation of the "sticky footer" concept. The margin is being set via script: var body = $( 'body' ), _window = $( window ); /** * Adds a top margin to the footer if the sidebar widget area is higher * than the rest of the page, to help the footer always visually clear * the sidebar. ...


4

Take a look in your widgets. Appearance > Widgets, and remove the ones you don't want.


4

widgets_init isn't a function, it's an action hook. The callback you specify for that hook is generate_sidebars which requires two parameters, but the widgets_init hook doesn't pass any parameters to its callbacks. I think what you're trying to do is this: add_action( 'widgets_init', 'call_sidebar_function' ); Which will call the call_sidebar_function() ...


4

There is no 'default sidebar area' in WordPress. It's entirely up to themes to register whatever widget areas they need. So you will need to use register_sidebar() to create the areas for widgets to be added ('sidebars'). Then in your theme you need to use dynamic_sidebar() to output those widgets. The get_sidebar() function is the equivalent of get_header()...


3

What about using something simple like a plugin. I'm using Widgets Controller and it's a good one. A plugin that give you control for show or hide widgets on WordPress Categories, Posts and Pages.


3

WooCommerce have a sidebars plugin: http://www.woothemes.com/woosidebars/ This will give you really comprehensive control- you can create new sidebar areas for whatever pages you like.


3

The widget_display_callback hook takes 3 arguments as we can see in /wp-includes/widgets.php: (line 180, WP 3.4.2) $instance = apply_filters('widget_display_callback', $instance, $this, $args); So, to make a complete hook, the code is: // Priority = 10 // Arguments = 3 add_filter('widget_display_callback', 'wpse_57546_widget_display_callback', 10, 3); ...


3

Thanks for sharing your solution. I have used what has been described in this question to create a piece of code that can be used to initialize sidebars very easily. It is very flexible, you can create as many widgets as you want without having to modify the code at all. Just make use of the filter hooks and pass in arguments in an array. Here is the ...


Only top voted, non community-wiki answers of a minimum length are eligible