• Hi!
    I’ve tried several ideas to get this to work but, alas without any luck.

    The following code creates a “select” option which allows me to assign a category to act as a portfolio.

    <select name="portfolio_cat" id="portfolio_cat">
    	<option value="none" <?php if ($theme_options["portfolio_cat"]=="none"):?> selected <?php endif; ?>>none</option>
    	<?php $p_cats = get_categories('hide_empty=0');
    	foreach ($p_cats as $portfolio_cat)
    	:?>
    	<option value="<?php echo $portfolio_cat->cat_ID ?>" <?php if ($this->options["portfolio_cat"]==$portfolio_cat->cat_ID):?> selected <?php endif; ?>>
    	<?php echo $portfolio_cat->cat_name ; ?>
    	</option>
    <?php endforeach; ?>
    </select>

    What I want is to have checkboxes instead, so I can assign several categories, instead of just one.

    Any help is greatly appreciated!

    Cheers
    /v0ice

Viewing 1 replies (of 1 total)
  • Never had the time to respond yesterday, but i bookmarked your thread incase noone provided an answer.

    All you need do is switch the option to store an array of values, and loop over the terms/categories creating checkboxes, whilst comparing their ID to that of those stored in the current saved setting… etc etc.. (easier if you just examine the code below).

    Try this.. (remove the code you posted above, and put this in it’s place)

    <?php
    $portfolio_cats = ( is_array( $theme_options['portfolio_cat'] ) ) ? array_map( 'absint', $theme_options['portfolio_cat'] ) : array();
    $_cats_available = get_terms( 'category', array( 'hide_empty' => 0 ) );
    
    if( $_cats_available ) {
    	foreach( $_cats_available as $term ) {
    		?>
    		<label class="selectit">
    			<input value="<?php echo absint( $term->term_id ); ?>"<?php checked( in_array( absint( $term->term_id ),$portfolio_cats ),true ); ?> name="portfolio_cat[]" type="checkbox"> <?php esc_html_e( $term->name ); ?>
    		</label>
    		<?php
    	}
    }
    ?>

    It should work as is, obviously the current saved value(for that setting) won’t be an array, but don’t worry, just go ahead add the new code, check the categories as appropriate, and save, it should sort itself out.

    There could possibly be other changes you’ll need to make elsewhere if other code has expectations for $theme_options["portfolio_cat"] to be a singular numeric value. You should see errors if this is the case, but the good thing about these errors is they will indicate exactly where else you need to modify code to account for the new changes (i can help if necessary).

    Hope that helps.. ??

Viewing 1 replies (of 1 total)
  • The topic ‘PHP – multiple checkbox options’ is closed to new replies.