Forums

[resolved] Using 'register_sidebar' and adding a second sidebar on the right. (10 posts)

  1. RedeemedLife
    Member
    Posted 1 year ago #

    Hey everyone.

    I'd made a thread on this earlier, but the question of the thread was answered, so I'm making a new one.

    What my issue has been, has been every time I add a widget to the sidebar, I lose the CSS formatting on the sidebar.

    The sidebar.php file has a few of the widgets hard coded into it, with the 'BlockContent' class applied to them, that adds a background and border to the widget, but whenever I go into the admin section of the blog and add a widget on the sidebar (even adding just the ones that are there already, coded into sidebar.php) the formatting disappears and looking at the code, the "BlockContent" divs are replaced with calendar-3 pages-3, categories-3, archives-3, and linkcat-2.

    What people said was to use register_sidebar, but I'm not sure where to put the code, and what to change the code to, to make it so any widgets have the 'BlockContent' CSS rule.

    I'm also wondering how I add a second sidebar to the site, on the right, and have two sidebars show up under the admin 'widget' section.

    Thanks so much, Any help would be great!

    For troubleshooting, here's my current code:

    sidebar.php:

    <div class="Left">
    
    <?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar() ) : else : ?>
    
        <br />
        <div class="BlockContent">
        <h2><?php _e('Calendar'); ?></h2>
            <?php get_calendar(); ?>
        </div>
        <br />
        <div class="BlockContent">
        <?php wp_list_pages('depth=3&title_li=<h2>Pages</h2>'); ?>
        </div>
        <br />
        <div class="BlockContent">
        <h2><?php _e('Categories'); ?></h2>
    
        <?php wp_list_cats('sort_column=name&optioncount=1&hierarchical=0'); ?>
        </div>
        <br />
        <div class="BlockContent">
        <h2><?php _e('Archives'); ?></h2>
    
                <?php wp_get_archives('type=monthly'); ?>
        </div>
        <br />
        <div class="BlockContent">
        <?php get_links_list(); ?>
        </div>
        <br />
        <div class="BlockContent">
        <h2><?php _e('Meta'); ?></h2>
    
               <?php wp_register(); ?>
            <?php wp_loginout(); ?>
            <?php wp_meta(); ?>
        </div>
    
    <?php endif; ?>
    
    </div>

    functions.php:

    <?php
    $args = array(
        'name'          => sprintf(__('Sidebar %d'), $i ),
        'id'            => 'sidebar-$i',
        'description'   => '',
        'before_widget' => '<li id="%1$s" class="BlockContent %2$s">',
        'after_widget'  => '</li>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>' );
    if ( function_exists('register_sidebar') ) register_sidebar($args);
    ?>

    index.php:

    <?php get_header(); ?>
    
    <?php get_sidebar(); ?>
    
    <div id="Content">
    
        <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    
                <div class="post">
    
                    <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    
                    <div class="entry">
    
                        <?php the_content(); ?>
    
                        <p class="postmetadata">
    
    <?php _e('Filed under:'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php  the_author(); ?><br />
    
    <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
    
                        </p>
    
                    </div>
    
                </div>
    
        <?php endwhile; ?>
    
            <div class="navigation">
                <?php posts_nav_link(); ?>
            </div>
    
        <?php else : ?>
    
            <div class="post" id="post-<?php the_ID(); ?>">
                <h2><?php_e('Not Found'); ?></h2>
            </div>
    
        <?php endif; ?>
    
    </div>
    
    <?php get_footer(); ?>
    
    </div>
    
    </body>
    </html>
  2. RedeemedLife
    Member
    Posted 1 year ago #

    Hmm, I just found the following code on this site: Creating Multiple Dynamic Sidebars

    The code is:

    <?php
    if ( function_exists('register_sidebar') )
    register_sidebar(array('name'=>'sidebar1',
    'before_widget' => '',
    'after_widget' => '',
    'before_title' => '<h4>',
    'after_title' => '</h4>',
    ));
    register_sidebar(array('name'=>'sidebar2',
    'before_widget' => '',
    'after_widget' => '',
    'before_title' => '<h4>',
    'after_title' => '</h4>',
    ));
    ?>

    I can see where it's mapping the two sidebars, but how to I edit this code to make it so the sidebars use the 'BlockContent' CSS rules?

  3. alchymyth
    The Sweeper
    Posted 1 year ago #

    as your hard-coded sidebar does use a div for the widgets, and not list elements, try:

    <?php
    $args = array(
        'name'          => sprintf(__('Sidebar %d'), $i ),
        'id'            => 'sidebar-$i',
        'description'   => '',
        'before_widget' => '<div id="%1$s" class="BlockContent %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>' );
    if ( function_exists('register_sidebar') ) register_sidebar($args);
    ?>

    - to add a sidebar to the right would require quite a few steps:

    1. creating a new sidebar file; for instance: sidebar-right.php;
    the code could be similar to the existing sidebar.php; but with at least a different css class to allow for independant positioning.

    2. calling this file from your template file:
    <?php get_sidebar('right'); ?>
    in a location which is suitable to be styled with css;

    3. registering the right sidebar in functions.php:

    <?php
    $args = array(
        'name'          => sprintf(__('Sidebar %d'), $i ),
        'id'            => 'sidebar-$i',
        'description'   => '',
        'before_widget' => '<div id="%1$s" class="BlockContent %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>' );
    if ( function_exists('register_sidebar') ) register_sidebar($args);
    ?>

    4. making new styles in style.css for the positioning of the right sidebar;
    also adapting existing styles to allow for the space of the right sidebar.

  4. RedeemedLife
    Member
    Posted 1 year ago #

    Ok, so I make my functions.php say this (From what I could see, both code snippets look identical):

    <?php
    $args = array(
        'name'          => sprintf(__('Sidebar %d'), $i ),
        'id'            => 'sidebar-$i',
        'description'   => '',
        'before_widget' => '<div id="%1$s" class="BlockContent %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>' );
    if ( function_exists('register_sidebar') ) register_sidebar($args);
    ?>

    ...and I just make another sidebar file (I'm going to call it sidebar-right.php) with identical code?

    Do I need to edit the second sidebar and the first to distinguish them from each other? For example, do I need to edit the line of code "<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar() ) : else : ?>" at the top for the two sidebars to make them different?

    Do I also have to do anything extra to make the sidebars show up in the widget admin section?

  5. RedeemedLife
    Member
    Posted 1 year ago #

    Ok, So I edit my functions.php so that it says:

    <?php
    $args = array(
        'name'          => sprintf(__('Sidebar %d'), $i ),
        'id'            => 'sidebar-$i',
        'description'   => '',
        'before_widget' => '<div id="%1$s" class="BlockContent %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>' );
    if ( function_exists('register_sidebar') ) register_sidebar($args);
    ?>

    ...but I think I may have broken something else, because not only do two sidebars not show up on the site or in the admin widget section, the only sidebar showing in the admin widget section is 'Sidebar 0' and if I add a widget to that, it doesn't reflect it on the site.

    Here's my sidebar.php and sidebar-right.php code:

    <div class="Left">
    
    <?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar() ) : else : ?>
    
        <br />
        <div class="BlockContent">
        <h2><?php _e('Calendar'); ?></h2>
            <?php get_calendar(); ?>
        </div>
        <br />
        <div class="BlockContent">
        <?php wp_list_pages('depth=3&title_li=<h2>Pages</h2>'); ?>
        </div>
        <br />
        <div class="BlockContent">
        <h2><?php _e('Categories'); ?></h2>
    
        <?php wp_list_cats('sort_column=name&optioncount=1&hierarchical=0'); ?>
        </div>
        <br />
        <div class="BlockContent">
        <h2><?php _e('Archives'); ?></h2>
    
                <?php wp_get_archives('type=monthly'); ?>
        </div>
        <br />
        <div class="BlockContent">
        <?php get_links_list(); ?>
        </div>
        <br />
        <div class="BlockContent">
        <h2><?php _e('Meta'); ?></h2>
    
               <?php wp_register(); ?>
            <?php wp_loginout(); ?>
            <?php wp_meta(); ?>
        </div>
    
    <?php endif; ?>
    
    </div>

    (The only different between the two is the div class on the first line.)

    ...and here's the code in my functions.php:

    <?php
    $args = array(
        'name'          => sprintf(__('Sidebar %d'), $i ),
        'id'            => 'sidebar-$i',
        'description'   => '',
        'before_widget' => '<div id="%1$s" class="BlockContent %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>' );
    if ( function_exists('register_sidebar') ) register_sidebar($args);
    ?>
  6. alchymyth
    The Sweeper
    Posted 1 year ago #

    in functions.php, try:

    <?php
    $args = array(
        'name'          => 'Sidebar 1',
        'id'            => 'sidebar-1',
        'description'   => '',
        'before_widget' => '<div id="%1$s" class="BlockContent %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>' );
    if ( function_exists('register_sidebar') ) register_sidebar($args);
    ?>
    <?php
    $args = array(
        'name'          => 'Sidebar 2',
        'id'            => 'sidebar-2',
        'description'   => '',
        'before_widget' => '<div id="%1$s" class="BlockContent %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>' );
    if ( function_exists('register_sidebar') ) register_sidebar($args);
    ?>

    in your 'old' sidebar.php, try:

    <?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(1) ) : else : ?>

    and in sidebar-right.php, try:

    <?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(2) ) : else : ?>
  7. RedeemedLife
    Member
    Posted 1 year ago #

    Wow, I think that did it!

    I see the sidebar on the left kept the 'BlockContent' CSS settings, and I don't see the sidebar on the right yet, but I'm pretty sure that's because I need to edit my index.php to call it.

    Thanks so much! I'm going to hit the index file and see if I can get it...

  8. RedeemedLife
    Member
    Posted 1 year ago #

    Ok, here's my index.php file:

    <?php get_header(); ?>
    
    <?php get_sidebar(); ?>
    
    <div id="Content">
    
        <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    
                <div class="post">
    
                    <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    
                    <div class="entry">
    
                        <?php the_content(); ?>
    
                        <p class="postmetadata">
    
    <?php _e('Filed under:'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php  the_author(); ?><br />
    
    <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
    
                        </p>
    
                    </div>
    
                </div>
    
        <?php endwhile; ?>
    
            <div class="navigation">
                <?php posts_nav_link(); ?>
            </div>
    
        <?php else : ?>
    
            <div class="post" id="post-<?php the_ID(); ?>">
                <h2><?php_e('Not Found'); ?></h2>
            </div>
    
        <?php endif; ?>
    
    </div>
    
    <?php get_footer(); ?>
    
    </div>
    
    </body>
    </html>

    I'm looking at the line on top <?php get_sidebar(); ?>, do I need to change that to:

    <?php get_sidebar(1); ?>

    ...and put:

    <?php get_sidebar(2); ?>

    ...where I want the second sidebar to be?

  9. alchymyth
    The Sweeper
    Posted 1 year ago #

    you leave the existing line as it is,
    and add a new
    <?php get_sidebar('right'); ?>
    where you want to have the right sidebar.

  10. RedeemedLife
    Member
    Posted 1 year ago #

    Wow! It really worked! Thanks so much!

Topic Closed

This topic has been closed to new replies.

About this Topic