WordPress Function to Add parent page slug to body_class

Add parent page slug to body_class

/***************************************************************
* Function body_class_section
* Add the top level page to the body class for coloured sections
***************************************************************/

add_filter('body_class','body_class_section');

function body_class_section($classes) {
    global $wpdb, $post;
    if (is_page()) {
        if ($post->post_parent) {
            $parent  = end(get_post_ancestors($current_page_id));
        } else {
            $parent = $post->ID;
        }
        $post_data = get_post($parent, ARRAY_A);
        $classes[] = 'section-' . $post_data['post_name'];
    }
    return $classes;
}

This filter adds a unique body class based on the highest level parent of the current page. I use this for sites that have specific colours or layouts for each section of the site. It works best with sites based around pages. CSS examples:

.section-about { background: red; }
.section-portfolio { background: blue; }

Your theme must also make use of the body_class function.

Fix oEmbed flash objects

/***************************************************************
* Function my_oembed_wmode
* Fix oEmbed window mode for flash objects
***************************************************************/

add_filter('embed_oembed_html', 'my_oembed_wmode', 1);

function my_oembed_wmode( $embed ) {
    if ( strpos( $embed, '<param' ) !== false ) {
        $embed = str_replace( '<embed', '<embed wmode="transparent" ', $embed );
        $embed = preg_replace( '/param>/', 'param><param name="wmode" value="transparent" />', $embed, 1);
    }
    return $embed;
}

I have had problems in the past where oEmbed Flash objects collide with drop down navigation menus. This filter fixes that problem by adding a transparent window mode to the embeds.

Remove the “comments” column from Admin page list

/***************************************************************
* Function custom_pages_columns
* Remove "comments" from pages overview (rarely use comments on pages)
***************************************************************/

add_filter('manage_pages_columns', 'custom_pages_columns');

function custom_pages_columns($defaults) {
    unset($defaults['comments']);
    return $defaults;
}

I hardly ever make use of commenting on pages and this helps keep things tidy in the WordPress dashboard.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *