Is this the best (and only) option to style CSS for a given post type?

You’re absolutely right in your assumption. CSS should never be tied to post IDs, because they can always change (for example if you decide to migrate your posts to another install).

You can always shift control towards the admin dashboard.

Add a custom field in a post/page that you want styled in a special way (in your example you would have the same custom field in all language versions of the post/page). Let’s say it will be wpse_custom_class. Add it to each post/page; the value will be portfolio for this example.

Add this code to functions.php (copied from this answer). When the page loads it will look for a custom field and insert its value as a CSS class on the HTML body element.

function wpse_filter_post_class( $classes ) {
    $my_post_class = get_post_meta($post->ID, "wpse_custom_class");

    if( $my_post_class != '' ) {
        $classes[] = $my_post_class;
    }
    return $classes;
}

add_filter( 'post_class', 'wpse_filter_post_class' );

Your designer can now use it like so:

.portfolio #content .main {
    max-width: 100%;
}

This is more work on the admin side (you or the designer will need to manually go in and specify those CSS classes for all the custom-styled pages), but ultimately it contributes to a very flexible and future-proof solution.

Leave a Comment