• Hello

    My goal is to have my dynamic sidebar display alternating h2 classes for each h2 class that is written. So if I have a sidebar widget called “Categories” and one called “Recent Posts”, I want each of those titles to be enclosed in their own h2 tag, and I want each one to display a different h2 class.

    I have the first part accomplished, which I did with this code in the “functions.php” file:

    <?php
    if ( function_exists('register_sidebar') )
    register_sidebar(array(
    'before_widget' => '',
    'after_widget' => '',
    'before_title' => '<h2>',
    'after_title' => '</h2>',
    ));
    ?>

    So then I wrote h2 classes for “odd” and for “even” alternating widgets:

    #sidebar h2.odd{background:url(images/blog-sidebar-blue.jpg);}
    #sidebar h2.even{background:url(images/blog-sidebar-red.jpg);}

    Then I tried to use the toggle property in my functions.php file to force the h2 styles to alternate between odd and even as they are dynamically written in the sidebar:

    <?php
    if ( function_exists('register_sidebar') )
    register_sidebar(array(
    'before_widget' => '',
    'after_widget' => '',
    'before_title' => '<h2 class="<?php echo ( ( $toggle = !$toggle ) ? 'odd' : 'even' ); ?>">',
    'after_title' => '</h2>',
    ));
    ?>

    Does anyone know why this doesn’t work or have suggestions about a better way to accomplish my goal? I might be barking up totally the wrong tree here, so I’m open to any suggestions. Thank you!

  • The topic ‘Dynamic Sidebar: Toggle h2 Tag Classes’ is closed to new replies.