SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic Wordpress Sidebar: Toggle h2 Tag Classes

    I am working in WordPress 2.8.6, but I think this is actually more of a general PHP question. My goal is to have my dynamic sidebar display alternating h2 classes for each h2 class that is written by the blog software (it writes a new one for each sidebar widget). 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:

    Code PHP:
    <?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:

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


    So 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:

    Code PHP:
    <?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!

  2. #2
    SitePoint Zealot j3ph's Avatar
    Join Date
    Jan 2006
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

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

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much for your reply.

    So with the code you provided, Wordpress is actually not even reading the opening h2 tag any more. When I view the source code for it, here's the markup it produced:

    Code HTML4Strict:
    <ul id="sidebar">
    oddCategories</h2>
    <li>List Item 1</li>
    </ul>

    Instead of what I'm hoping for it to look like:

    Code HTML4Strict:
    <ul id="sidebar">
    <h2 class="odd">Categories</h2>
    <li>List Item 1</li>
    </ul>

    Hopefully that helps with diagnosing the problem. Thanks!!

  4. #4
    SitePoint Zealot j3ph's Avatar
    Join Date
    Jan 2006
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I need to see more code. Can you share the line that $toggle gets assigned a value? Also, which template file are you putting this code in?

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Again, thank you for your reply.

    I am not assigning a value to $toggle. I have been experimenting with different code combinations in the functions.php file and the sidebar.php file. So, just so you see everything I'm working with, here is all the code that I currently have in the sidebar.php file:

    Code PHP:
    <ul id="sidebar">
     
    <?php if ( !function_exists('dynamic_sidebar')
            || !dynamic_sidebar() ) : ?>
    	<li>
            <a href="#"><h4>Default</h4></a>
            <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Mauris risus. Morbi mauris.</p> 
        </li>
       <?php endif; ?>
    </ul>

    And here is everything that I have in the functions.php file:

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

    As you know, it is that code in functions.php that I have unsuccessfully tried to manipulate to display my alternate h2 classes. Hopefully that helps you diagnose the issues. Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •