0

I am trying to keep the classes the exact same on my normal (blog) posts, and a custom post type I have called "locations".


When I post a "normal post" I get the following classes:

class="post-# post type-post status-publish format-gallery hentry post_format-post-format-gallery"


But when I post a "location post" I get:

class="post-92 posts locations type-locations status-publish format-gallery hentry"


I cannot figure out how to change location to posts or format-gallery to post_format-post-format-gallery.


I can go into "content.php" and change it to "post_class('post');" But that just adds one class and it doesn't help if I have to choose a format other than "gallery".

I feel like there is an easier way to do this...

Please let me know if you need more information, like specific file code. I am using the Fukasawa theme. http://www.andersnoren.se/themes/fukasawa/

3

As documented here: https://codex.wordpress.org/Function_Reference/post_class#Add_Classes_By_Filters

You can add a hook to the 'post_class' filter to add/remove classes in the array passed. You can use the various template tags like has_post_format() and others to see what classes you need to add; you'll have access to the ID of the post in question.

You could erase the array entire and start fresh if you wanted, though it may have unforeseen side effects depending on what classes your stylesheet uses.

function rewrite_post_class( $classes, $class, $post_id ) {
    // Add a class
    $classes[] = 'my-custom-class';

    // Remove a class
    if ( $index = array_search( 'unwanted-class', $classes ) ) {
        unset( $classes[ $index ] );
    }

    // Start over
    $classes = array( $class );
    $classes[] = get_post_type( $post_id );
}
add_filter( 'post_class', 'rewrite_post_class', 10, 3 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.