I am currently using Wordpress to create and populate a <select>
with <option>
s.
<?php if( $lh_loop->have_posts() ): ?>
<select class="mousechoice left">
<?php while( $lh_loop->have_posts() ) : $lh_loop->the_post();?>
<optgroup label="<?php echo get_field("company_name") ?>">
<option data-id="<?php echo $post->post_name; ?>"><?php the_title(); ?></option>
</optgroup>
<?php endwhile; ?>
</select>
<?php endif; ?>
Which is showing this:
<select class="mousechoice left">
<optgroup label="SteelSeries">
<option data-id="sensei">Sensei</option>
</optgroup>
<optgroup label="Razer">
<option data-id="deathadder">Deathadder</option>
</optgroup>
<optgroup label="SteelSeries">
<option data-id="kana">Kana</option>
</optgroup>
</select>
I would like a way of grouping posts with the same company name into an <optgroup>
. Which would result in this:
<select class="mousechoice left">
<optgroup label="SteelSeries">
<option data-id="sensei">Sensei</option>
<option data-id="kana">Kana</option>
</optgroup>
<optgroup label="Razer">
<option data-id="deathadder">Deathadder</option>
</optgroup>
</select>
I have tried comparing the current post's company to the previous post's company but realised that a post with the same company name could be after or before another post with a different company name.
I know that I could also use if statements to specify each company name but I would like a solution that would allow for other company names not yet input into Wordpress.