0

According to this link…

https://developer.wordpress.org/reference/functions/post_class/#Add_Classes_By_Filters

…i can add a filter to change the class(es) of a post. How can I do this from a theme? I just have the functions.php and adding this code there doesnt change anything. currently my single posts look like this:

<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
  <div class="wp-block-group has-light-color-background-color has-background is-layout-constrained wp-container-core-group-is-layout-7 wp-block-group-is-layout-constrained">
    (content of a single post)
  </div>
</div>

I'd like my child-theme to add a class to one of those divs. But adding the linked filter-code to the functions.php doesnt do anything.

I added this code to the functions.php to no avail:

function rewrite_post_class( $classes, $class, $post_id ) {
    $classes[] = 'my-custom-class';
    return $classes;
}
add_filter( 'post_class', 'rewrite_post_class', 10, 3 );
2
  • You meant the post_class hook not the post_class function? Can you show us your attempt at the function and the add_filter() line please? There's nothing at your link. That said, I think that will affect the div that already has the post-(id) class, not the two you've shown
    – Rup
    Commented Aug 2 at 9:05
  • @Rup I updated the question
    – rhavin
    Commented Aug 2 at 11:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.