WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add new class to page body (3 posts)

  1. zzuum
    Member
    Posted 3 months ago #

    I want to add new class on each new page.
    for example:
    When i create new page, i want to add page-slug to class in <body> tag.

    So that page-slug be my new class.

  2. zzuum
    Member
    Posted 3 months ago #

    Here si solution:

    //Page Slug Body Class
    function add_slug_body_class( $classes ) {
    global $post;
    if ( isset( $post ) ) {
    $classes[] = $post->post_type . '-' . $post->post_name;
    }
    return $classes;
    }
    add_filter( 'body_class', 'add_slug_body_class' );
  3. Jose Castaneda
    Member
    Posted 3 months ago #

    WordPress already adds the id to the body class.

    Sort of like:

    <body class="page page-id-124 page-template-default">

    Depending on how the theme functions. :)

Reply

You must log in to post.

About this Topic

Tags