A PHP snippet for Dynamic, URL-Based Conditional Logic Using wp_parse_url().
function add_custom_body_class_based_on_url($classes) {
$current_url = home_url($_SERVER['REQUEST_URI']);
$url_parts = wp_parse_url($current_url);
if (!empty($url_parts['path'])) {
$path_segments = explode('/', trim($url_parts['path'], '/'));
// Check if a certain segment is present.
if (in_array('special-page', $path_segments)) {
$classes[] = 'custom-special-page';
}
}
return $classes;
}
add_filter('body_class', 'add_custom_body_class_based_on_url');