• Once one could have single posts with sidebar in Twenty Eleven using the plugin “Twenty Eleven Theme Extensions”, but this plugin does not work anymore starting with PHP 8.

    I found a workaround by making a change to the function.php of the parent theme – in function twentyeleven_body_classes( $classes ), second if statement [if ( is_singular() …].

    I would prefer to override this function in my child theme, but the function twentyeleven_body_classes( $classes ) lacks the necessary “if ( function_exists …” statement.

    I would appreciate if you could add this to a future update of Twenty Eleven.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator threadi

    (@threadi)

    The function you mentioned uses a WordPress hook. You can also use this to change the class specifications.

    I’m not sure what exactly you want to achieve, but I assume you don’t want the “singular” class on the body if an additional condition is not met. In that case, I would recommend

    a) removing the class using the hook.

    b) then resetting it if your condition applies.

    For example (untested):

    function twentyeleven_body_classes_replace_singular( $classes ) {
    // find the entry with the class "singular".
    $singular_key = array_search( 'singular', $classes, true );

    // remove the entry.
    if( $singular_key !== false ) {
    unset( $classes[ $singular_key ] );
    }

    // now define your custom condition and add the class again.
    // !!!

    // return the resulting list of classes.
    return $classes;
    }
    add_filter( 'body_class', 'twentyeleven_body_classes_replace_singular' );
    Thread Starter Robert Poth

    (@rpweb777)

    Thanks for your suggestion, I assume this should work in the child theme’s function.php; I will give it a try.

    Problem and my workaround, which works but is overwritten with each update of the parent theme:

    Twenty Eleven has a template single.php for displaying all single posts which doesn’t include the sidebar. With the function twentyeleven_body_classes( $classes ), the div classes .singular are applied to single posts.

    1. Create a template single-sidebar.php for all single posts which includes the sidebar
    2. Add this template in the second if-statement of the function twentyeleven_body_classes( $classes ) to prevent the div classes .singular being applied to single posts. [&& ! is_page_template ( ‘sidebar-single.php’ )]

    I use the method below to add the sidebar to both pages and single posts in Twenty Eleven. No need for a plugin and thanks to the use of a child theme it remains intact after updating.

    1.
    Create a child theme containing functions.php, page.php, and single.php.

    2.
    Add this code to functions.php:

    // Twenty Eleven - Show sidebar on pages and single posts as well 
    add_filter('body_class', 'fix_body_class_for_sidebar', 20, 2);
    function fix_body_class_for_sidebar($wp_classes, $extra_classes) {
    if( is_single() || is_page() ){
    if (in_array('singular',$wp_classes)){
    foreach($wp_classes as $key => $value) {
    if ($value == 'singular')
    unset($wp_classes[$key]);
    }
    }
    }
    return array_merge($wp_classes, (array) $extra_classes);
    }

    3.
    Add this call for the sidebar at the bottom of the files page.php and single.php (just above the call for the footer):

    <?php get_sidebar(); ?>

    4.
    Done.

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.