Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - Different sidebar for each page

Tags:

wordpress

Client needs an website to migrate into WordPress.

In this website each page has an sidebar with different content inside it

In some page accordion comes under sidebar, in some just text and images are visible

How to implement this in WordPress?

If template has to be created, there are many page it cant be done so

Even for every page different sidebar widget is not also possible

Guide me a way to implement this

like image 473
Sangeetha Mani Avatar asked Mar 19 '14 09:03

Sangeetha Mani


3 Answers

A different sidebar (widget) are can be added to each page in two steps:

  1. Add the sidebar to the theme template using the page slug as part of the sidebar name. This ensures that the sidebar has a unique name for that page.
  2. Register the sidebars for each page in functions.php for your theme

Add the sidebar to the theme template

In your theme template, add the following code where you want the widget area to be:

<?php
    global $post;
    dynamic_sidebar( 'widget_area_for_page_'.$post->post_name );
?>

Register the sidebars

In functions.php for your theme add the following block of code to register the sidebars for each page in your site. Note that it includes draft pages etc so that you can edit the widgets while still in draft mode.

function myTheme_registerWidgetAreas() {
    // Grab all pages except trashed
    $pages = new WP_Query(Array(
        'post_type' => 'page',
        'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit'),
        'posts_per_page'=>-1
    ));
    // Step through each page
    while ( $pages->have_posts() ) {
        $pages->the_post();
        // Ignore pages with no slug
        if ($pages->post->post_name == '') continue;
        // Register the sidebar for the page. Note that the id has
        // to match the name given in the theme template
        register_sidebar( array(
            'name'          => $pages->post->post_name,
            'id'            => 'widget_area_for_page_'.$pages->post->post_name,
            'before_widget' => '',
            'after_widget'  => '',
            'before_title'  => '',
            'after_title'   => '',
        ) );
    }
}
add_action( 'widgets_init', 'myTheme_registerWidgetAreas' );

Hope it helps!

like image 125
Projectitis Avatar answered Sep 20 '22 17:09

Projectitis


There's a great plugin for this:

https://wordpress.org/plugins/custom-sidebars/

Sometimes it is necessary to show different elements on the sidebars for some posts or pages. The themes nowadays give you some areas to put the widgets, but those areas are common for all the posts that are using the same template. NOTE: You need to use a theme that accepts widgets to make this plugin work.

Screenshot

I think this is what you're looking for.

like image 40
Kees Sonnema Avatar answered Sep 23 '22 17:09

Kees Sonnema


This can be done with the https://wordpress.org/plugins/jetpack/ plugin. Once activated you can choose what widgets display on what pages:

The Widget Visibility module enables you to configure widgets to appear only on certain pages (or be hidden on certain pages) by using the Visibility panel.

enter image description here

Visibility is controlled by five aspects: page type, category, tag, date, and author. For example, if you wanted the Archives widget to only appear on category archives and error pages, choose “Show” from the first dropdown and then add two rules: “Page is 404 Error Page” and “Category is All Category Pages.”

enter image description here

like image 23
Howli Avatar answered Sep 20 '22 17:09

Howli