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

  1. zzuum
    Posted 1 year 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
    Posted 1 year 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. 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. :)

Topic Closed

This topic has been closed to new replies.

About this Topic