dimanche 14 janvier 2018

How to target blog page vs non-blog page - wordpress bad logic?

I want to check if page has its sidebar if yes add has-page-sidebar or no-page-sidebar

AND separately if it is a blog page AND not a non-blog page I want to check if the blog page has a sidebar if yes add has-blog-sidebar if not add no-blog-sidebar class to the body.

At the moment, on the non-blog page the the code is adding no-blog-sidebar to the non-blog. Which i don't want because it just display no-page-sidebar only

What is wrong with the logic?

    function mock2_body_classes( $classes ) {
    // Adds a class of hfeed to non-singular pages.
    if ( ! is_singular() ) {
        $classes[] = 'hfeed';
        // by adding archive-view to body class I can target all the pages that archived i.e. category, date, search(?)
        $classes[] = 'archive-view';

    }

    if( is_front_page()) {

        // by adding frontpage-view to body class I can target the front page
        $classes[] = 'frontpage-view';  
    }

    // Add a class telling us if the sidebar is in use.
    if(is_active_sidebar('blog-sidebar') && !is_page() ) {
        $classes[] = 'has-blog-sidebar';
    } else {
        $classes[] = 'no-blog-sidebar';
    }

    // Add a class telling us if the page sidebar is in use.
    if ( is_active_sidebar( 'sidebar-2' ) && is_page() && ! is_home() ) {
        $classes[] = 'has-page-sidebar';
    } else {
        $classes[] = 'no-page-sidebar';
    }

    return $classes;
}
add_filter( 'body_class', 'mock2_body_classes' );

Aucun commentaire:

Enregistrer un commentaire