Adding HTML class selector with PHP -



Adding HTML class selector with PHP -

i'm tyring build wordpress theme user can take available options.

let's have alternative user can take column width available options. in page.php, i'm using code add together class

<div class="<?php if ( my_column_width == '1' ) : ?> full-width <?php elseif ( my_column_width == '2' ) : ?> one-two <?php elseif ( my_column_width == '3' ) : ?> one-three">

usually set in single line, above code it' easier understand. have lot of options on theme, conditional statements create hard read code.

can tell me improve approach this? i'm hoping people utilize theme can understand logic when read code.

create function:

function get_my_option_class($width) { switch ($witdh) { case 2: $class = 'one-two'; break; case 3: $class = 'one-three'; break; case 1: default: $class = 'full-width'; } homecoming $class; }

and you'll this:

<div class="<?= get_my_option_class($my_column_width) ?>">

functions reusable , much improve hard-coding logic in templates. if code supposed run on php lower php 5.4, it's improve alter lastly line this:

<div class="<?php echo get_my_option_class($my_column_width) ?>">

this work in php < 5.4 if short_open_tag off.

php html

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -