WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Register sidebars for each Page with matching names (5 posts)

  1. Doodlebee
    Member
    Posted 5 years ago #

    I'm trying to figure out how to auto-generate widgets for a static Page. I'm pretty close - well actually, I've doe it pretty simply... but my issue is I'd like to have the sidebars in the "Widgets" are have names instead of numbers, so I can easily tell which one goes with what Page.

    This is what I have so far in my functions.php file:

    $pages = get_pages();
    $num = count($pages);
    
    if ( function_exists('register_sidebar') ) {
      register_sidebars($num, array(
                  'name'=> 'Sidebar %d',
                  'before_widget' => '<div id="%1$s" class="widget %2$s">',
                  'after_widget' => '</div>',
                  'before_title' => '<h3>',
                  'after_title' => '</h3>',
        ));
      }

    And with that, I have a sidebar registered in my Widgets area for each Page I have created (right now, I have 10 static Pages - and I have 10 sidebars showing up in the Widgets area) - but they are numbers - saying "Sidebar 1", "Sidebar 2," etc.

    What I'd *like* to do is pull in the Page *name* instead. I actually had that working too, sort of. Before the above, I had this:

    $pages = get_pages();
    if ( function_exists('register_sidebar') ) {
      foreach($pages as $p) {
    	$name = $p->post_title;
    	register_sidebar(array(
    	  'name'=> $name . ' Sidebar',
          'before_widget' => '<div id="%1$s" class="widget %2$s">',
          'after_widget' => '</div>',
          'before_title' => '<h3>',
          'after_title' => '</h3>',
        ));
      }
    }

    And the widgets *did* indeed register sidebars as "Pagename Sidebar" ("Pagename" being the title of all the Pages I have created), but on the front end of the site, I get an error message:

    Catchable fatal error: Object of class stdClass could not be converted to string in /wp-includes/classes.php on line 273

    So would anyone have any suggestions on how I can correct this to give me the *names* of the Pages, rather than numbers? Thanks :)

  2. jcow
    Member
    Posted 5 years ago #

    Not sure if you worked this out yet, but I had the same issue, and want to post if posterity if nothing else.

    It turns out that if you just wrap get_pages() in a function it works.

    In functions.php:

    // Sidebars
    $before_widget = '<li id="%1$s" class="widget %2$s">';
    $after_widget = '</li>'."\n";
    $before_title = '<h2 class="widgettitle">';
    $after_title = '</h2>'."\n";
    
    if (function_exists('register_sidebar'))
    	register_sidebar(array(
    		'name' => 'Home Sidebar',
    		'before_widget' => $before_widget,
    		'after_widget' => $after_widget,
    		'before_title' => $before_title,
    		'after_title' => $after_title));
    createPageSidebars();
    
    function createPageSidebars() {
    	if ( function_exists('register_sidebar') ) {
    		$pages = get_pages();
    		foreach ($pages as $page) {
    			register_sidebar(array(
    				'name' => $page->post_title .' Sidebar',
    				'before_widget' => $before_widget,
    				'after_widget' => $after_widget,
    				'before_title' => $before_title,
    				'after_title' => $after_title));
    		}
    	}
    }

    In sidebar.php:

    <ul id="sidebar">
    
    <?php // Dynamic Sidebars
    if (is_home() && function_exists('dynamic_sidebar') && dynamic_sidebar('Home Sidebar')) :
    
    elseif (function_exists('dynamic_sidebar') && dynamic_sidebar($wp_query->post->post_title.' Sidebar')) :
    
    else : // If there is no dynamic sidebar for this page
    	include(TEMPLATEPATH . '/sidebar-default.php');
    endif; ?>
    
    </ul><!--/sidebar-->
  3. jcow
    Member
    Posted 5 years ago #

    EDIT:

    If you do use the code I posted, you have to add a global call to the function in order to use the before/after variables:

    function createPageSidebars() {
    	global $before_widget, $after_widget, $before_title, $after_title;
  4. Doodlebee
    Member
    Posted 5 years ago #

    Awesome - thanks jcow :)

  5. Doodlebee
    Member
    Posted 5 years ago #

    Well, I'm coming back to update - I did test this out and it works great. *However* - if you delete a Page (or set it as "draft" or "pending") the sidebars shift to compensate. So it's not a *true* association with the Page you're on. So for the purposes of the original question, this is resolved - but a new problem was found.

    Now I'm working to try and figure out how to pull the database info out relative to the page name (make the name and slug match to pull the right info) *or* create some kind of function that also deletes the associated widget stuff in the database when a Page is deleted.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.