• I’m trying to use a switch with the in_category() function to display a heading before a list of categories on a post.

    I have it working like this with an if statement, I know a switch would be the correct way to write it, can anyone help me with the syntax? I’m not sure exactly how a switch is suppose to look

    if ( in_category( 'Alto Horns, Baritone and Euphoniums' )) {
    						  _e("<!--:en-->Alto Horns, Baritone and Euphoniums<!--:--> <!--:fr-->Cuivres Altos, Barytons et Euphoniums<!--:-->");
    				} else if ( in_category( 'Artist Trombones' )) {
    						  _e("<!--:en-->Artist Trombones<!--:--> <!--:fr-->Trombones de la gamme Artist<!--:-->");
    				} else if ( in_category( 'Bass Trombones' )) {
    						  _e("<!--:en-->Bass Trombones<!--:--> <!--:fr-->Trombones basse<!--:-->");
    				} else if ( in_category( 'Euphoniums' )) {
    						  _e("<!--:en-->Euphoniums<!--:--> <!--:fr-->Euphoniums<!--:-->");
    				} else if ( in_category( 'Flugelhorns' )) {
    						  _e("<!--:en-->Flugelhorns<!--:--> <!--:fr-->Bugles<!--:-->");
    				} else if ( in_category( 'Cornets' )) {
    						  _e("<!--:en-->Cornets<!--:--> <!--:fr-->Cornets<!--:-->");
    				}  else if ( in_category( 'Single and Double Horns' )) {
    						  _e("<!--:en-->Single and Double Horns<!--:--> <!--:fr-->Cors Francais et Cors Francais Double<!--:-->");
    				} else if ( in_category( 'Marching Brass' )) {
    						  _e("<!--:en-->Marching Brass<!--:--> <!--:fr-->Cuivres de marche<!--:-->");
    				} else if ( in_category( 'Pocket Trumpets' )) {
    						  _e("<!--:en-->Pocket Trumpets<!--:--> <!--:fr-->Trompettes de poche<!--:-->");
    				} else if ( in_category( 'Student Trombones' )) {
    						  _e("<!--:en-->Student Trombones<!--:--> <!--:fr-->Trombones d'&eacute;tudes<!--:-->");
    				} else if ( in_category( 'Trumpets' )) {
    						  _e("<!--:en-->Trumpets<!--:--> <!--:fr-->Trompettes<!--:-->");
    				} else if ( in_category( 'Tubas and Sousaphones' )) {
    						  _e("<!--:en-->Tubas and Sousaphones<!--:--> <!--:fr-->Tubas et Sousaphones<!--:-->");
    				} else if ( in_category( 'Valve Trombones' )) {
    						  _e("<!--:en-->Valve Trombones<!--:--> <!--:fr-->Trombone &agrave; pistons<!--:-->");
    				} else if ( in_category( 'XO Professional Trombones' )) {
    						  _e("<!--:en-->XO Professional Trombones<!--:--> <!--:fr-->Trombones professionnels XO<!--:-->");
    				} else if ( in_category( 'XO Professional Trumpets' )) {
    						  _e("<!--:en-->XO Professional Trumpet<!--:--> <!--:fr-->Trompettes professionnels XO<!--:-->");
    				}
Viewing 1 replies (of 1 total)
  • Hi, I’m not familiar with the in_category function, but the switch should look similar to this, the function is evaluated at the top and then looks for a matching case…
    You can also add a default case for no matches found.
    (More info from the PHP Manual https://php.net/manual/en/control-structures.switch.php)

    switch (in_category()) {
        case 'Alto Horns, Baritone and Euphoniums':
             _e("<!--:en-->Alto Horns, Baritone and Euphoniums<!--:--> <!--:fr-->Cuivres Altos, Barytons et Euphoniums<!--:-->");
            break;
        case 'Pocket Trumpets':
             _e("<!--:en-->Pocket Trumpets<!--:--> <!--:fr-->Trompettes de poche<!--:-->");
            break;
    }

Viewing 1 replies (of 1 total)
  • The topic ‘switch instead of list of if statements’ is closed to new replies.