Support » Themes and Templates » Check if widget title exists

  • Hello all, this is my first post here at WP and I am a little new at this but so far am really enjoying using WP.

    The first thing I cannot find in the docs is how to check if a Widget’s title is defined.

    The reason is that as it stands if there is no header defined the empty h4 tags still of course apply line-height which means even an empty title still takes up space, not a big deal but it interrupts the design a bit when there isn’t a title along with widgets that do have titles. Ideally i want to check for the title, if it doesn’t exist then I would not render a h4 tag.

    Also, I am not sure where this logic should be applied, if it belongs in the sidebar or functions files. Any help would be great.

    Scripts are as follows

    — [functions.php] —

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

    — [sidebar.php] —

    <?php if ( !function_exists('dynamic_sidebar')
    || !dynamic_sidebar() ) : ?>
    <?php endif; ?>

    — [index.php] —

    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar1') ) : ?>
    <?php endif; ?>

  • The topic ‘Check if widget title exists’ is closed to new replies.