Skip to content

Instantly share code, notes, and snippets.

@joshuaiz
Last active April 22, 2018 01:52
Show Gist options
  • Select an option

  • Save joshuaiz/619dcb5936b839972356a75c06efe6a8 to your computer and use it in GitHub Desktop.

Select an option

Save joshuaiz/619dcb5936b839972356a75c06efe6a8 to your computer and use it in GitHub Desktop.

Revisions

  1. joshuaiz revised this gist Apr 22, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions plate-body-class.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    <?php
    // Body Class functions
    // Adds more slugs to body class so we can style individual pages + posts.
    // In your SCSS/CSS, use .page-yourslug {} to add styles for that page or post.
    add_filter( 'body_class', 'plate_body_class' );

    function plate_body_class( $classes ) {
  2. joshuaiz created this gist Apr 22, 2018.
    59 changes: 59 additions & 0 deletions plate-body-class.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    // Body Class functions
    // Adds more slugs to body class so we can style individual pages + posts.
    add_filter( 'body_class', 'plate_body_class' );

    function plate_body_class( $classes ) {

    global $post;

    if ( isset( $post ) ) {

    /* $classes[] = $post->post_type . '-' . $post->post_name; *//*Un comment this if you want the post_type-post_name body class */
    $pagetemplate = get_post_meta( $post->ID, '_wp_page_template', true);
    $classes[] = sanitize_html_class( str_replace( '.', '-', $pagetemplate ), '' );
    $classes[] = $post->post_name;

    }

    if (is_page()) {

    global $post;

    if ( $post->post_parent ) {

    # Parent post name/slug
    $parent = get_post( $post->post_parent );
    $classes[] = $parent->post_name;

    # Parent template name
    $parent_template = get_post_meta( $parent->ID, '_wp_page_template', true );

    if ( !empty($parent_template) )
    $classes[] = 'template-'.sanitize_html_class( str_replace( '.', '-', $parent_template ), '' );

    }

    // If we *do* have an ancestors list, process it
    // http://codex.wordpress.org/Function_Reference/get_post_ancestors
    if ($parents = get_post_ancestors( $post->ID )) {

    foreach ( (array)$parents as $parent ) {

    // As the array contains IDs only, we need to get each page
    if ( $page = get_page($parent) ) {
    // Add the current ancestor to the body class array
    $classes[] = "{$page->post_type}-{$page->post_name}";
    }

    }

    }

    // Add the current page to our body class array
    $classes[] = "{$post->post_type}-{$post->post_name}";

    }

    return $classes;

    }