add_cssclass( string $class_to_add, string $classes ): string

Adds a CSS class to a string.

Parameters

$class_to_addstringrequired
The CSS class to add.
$classesstringrequired
The string to add the CSS class to.

Return

string The string with the CSS class added.

Source

function add_cssclass( $class_to_add, $classes ) {
	if ( empty( $classes ) ) {
		return $class_to_add;
	}

	return $classes . ' ' . $class_to_add;
}

Changelog

VersionDescription
2.7.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Usage when working over menus order

    $firstm = $lastm = false;
    $i = 0;
    
    foreach ( $menu as $order => $top ) {
            $i++;
    
            if ( 0 == $order ) {
                $menu[0][4] = add_cssclass( 'menu-top-first', $top[4] );
                $lastm = 0;
                continue;
            }
    
            if ( 0 === strpos( $top[2], 'separator' ) && false !== $lastm ) {
                // if separator
                $firstm = true;
                $c = $menu[ $lastm ][4];
                $menu[ $lastm ][4] = add_cssclass( 'menu-top-last', $c );
                continue;
            }
    
            if ( $first ) {
                $c = $menu[ $order ][4];
                $menu[ $order ][4] = add_cssclass( 'menu-top-first', $c );
                $firstm = false;
            }
    
            $lastm = $order;
        }

You must log in before being able to contribute a note or feedback.