Build Your Own Business Website header image

Whoops, you've found some premium content!

Watch the opening clip of this video to preview it,
the full video is available to paid members.

Part 10 – Dynamically Populate the Theme Options Select Box

Difficulty Level -

Filed Under Topics -

Listed Under Lesson Subjects -

Applies to -

The Genesis Child Theme Options Form is now customized so that the end user can select from a list of categories or tags. It’s easy enough to enter tag or category here but what’s perhaps harder is getting the term because somebody has to go over and figure out what the term id is for these.

What would be better is to be able to have a drop down that shows all of the categories and the end user can just pick from the category that they want. So that’s what we’re doing next and we’re doing that with this section here.

The First Option

We start off with this select id code just like we did before although it’s box1_term, not box1_taxonomy. For the first option we’re going to set it to nothing and we’re going to get rid of the PHP code altogether because all we really want to do have it act like a label.

This is sort of the default, the first one at the top of the list. We’re just going to say, “Choose One”. So at the top of the list is this area that says, “Choose One”, if it’s chosen it doesn’t return a value, the value is blank.

The Second Options

The second options are a bit of a pickle because we don’t know what the terms are. So what we have to do is go get all those terms. We also don’t know if it’s a post tag or if it’s a category so what we really need to do is go get the terms for each of those.

Get the Terms for Tags and Categories

The way we’re going to do that is to use the get_option function in WordPress. Actually, I’m just going to copy this section and explain it as we work through it.

Get the Options

So, $options = get_option(‘genesis-settings’). This is getting all of the Genesis options that have been set up and then $cat_terms is get_terms(‘category’) and $tag_terms = get_terms(‘post_tag’).

Get the Terms

The get_terms is a WordPress function that will return all of the terms in a category, all of the categories or all the tags. This is all the terms in the taxonomy category and all the terms in the taxonomy tag and it does so in what they refer to it as an object. Each category will have its name and its id and a bunch of other stuff associated with it when you get the option here.

You’re going to see how that matters here in a few minutes but we’re going to get the options so we’re going to get all the Genesis settings. We can’t use genesis_get_option because this is in the middle of actually setting the option so genesis_get_option won’t function.

What we have to use is the WordPress get_option to get all of the Genesis options. Then what we’re going to come here and say if ($options[‘box1_taxonomy’]). So if this ($options[‘box1_taxonomy’]) isn’t empty and if the ($options[‘box1_taxonomy’]) equals ‘post_tag’ then the terms we want are the $tag_terms. Otherwise, the terms you want are the $cat_terms.

That’s the first thing we’re doing here, deciding whether or not we want $tag_terms or $cat_terms. Then what we’re going to do is set up for each loop for the rest of these options.

And so instead of this, we’re going to go into PHP and say foreach($terms. So for each one of the terms, as $term which means that for each of these terms we’re going to cycle through all the terms and each term we cycle to a term we’re going to send it to the variable $term. So <?php foreach($terms as $term) and then we need an opening bracket for that and come down here and close that opening bracket.

So there are our opening and closing brackets for <?php foreach($terms as $term). Then what we’re going to do is create an option. And the option is kind of like this where

We’re going to say <?php echo $term->term_id. Let’s look at the get_terms function in WordPress for a second. It can take some arguments. We didn’t need any argument so I didn’t bother with that but it requires the name of the taxonomy.

Here’s what it returns. It returns an array of these values, Term_id, name, slug, term_group, term_taxonomy_id which is the same, taxonomy, description, parent and count. So it’s returning all of these.

We’re going to use term_id and name. So here we’re saying the value is term_id and then <?php selected is $term->term_id instead of post_tag and then genesis_get_option is going to be ‘box1_term’ and then we need the term name here so we’ll grab that again. Actually, we need <?php echo for this, $term and instead of term_id it’s name. So what we’ve done here is we said for each term we’re going to create an option out of each term where the value of that option is going to be its id and so if it’s selected it’s the id. The Genesis box option is just the same as before and then the choice that we’re going to give people is the $term->name.

Again, this is not particularly complicated but most people when they do tutorials like this don’t show you these kinds of options but this is really where it becomes valuable – when you can set up options that you don’t know in advance.

I was going to show you where you could first choose the post type then choose the taxonomy then choose the term but I felt I was going to take too long. It was getting too complicated so I decided not to do that but in any case, what you’re going to see here a list to choose from.

You can see you’ve got “Choose One” and then all of these category choices. For example, we could choose Bananas and save the settings, come over here and now it’s Bananas.

Save $200 on Membership Now!

Start learning today for as little as
$0.82 PER DAY!
THIS IS A LIMITED TIME OFFER!
Subscription Options
0 Comments… add one
0 comments… add one

Leave a Comment