We’ve created the Genesis Child Theme Options form but unfortunately what this requires so far is the user to know that it’s either a category or post tag and it requires them to know the id of the term that they want to display.
But let’s set this one up as if a person knows that – category, term and post to show. Then we’ll come back over here to our first group and we’re going to change up $args1. Because what we really want to do, if it’s cat then we want to say cat but if it’s tag then we want to say tag or post tag.
Let’s go back and look at WP_Query for a second. So if it’s tag, it’s tag or actually, we’re using tag_id because tag uses a slug, tag_id uses the id but cat uses the category id so you can see there’s an irritating sort of conflict here.
Put Options into Values and Add Choice of tag_id or cat.
So the first thing we have to do here is get the option and we can do this a couple of ways. We can use the WordPress get_options or we can use the genesis_get_option.
The genesis_get_option allows you to pick an individual option. If you look at this tutotrial’s ultimate result, he’s using genesis_get_option and then the option name which is not bad. we could do that here so we’ll use genesis_get_option.
We’re going to say $tax1 = genesis_get_option( ‘box1_taxonomy’ ) then $term1 = genesis_get_option( ‘box1_term’ ). Then finally, $posts1 = genesis_get_option( ‘box1_posts_to_show’ ).
Now what we can do is ask ourselves the question, if($tax1 === ‘post_tag’). These equal signs means equals this exactly. Then let’s just say $tax_name = ‘tag_id’ and then else $tax_name = ‘cat’.
So now we can come down to our $args and instead of ‘cat’ we can say $tax_name and instead of 4 we can say $term1, and then ‘posts_per_page’ is $posts1, okay.
So what we’ve done is we’ve taken our options, we’ve put them into values, we’ve decided whether or not we were going to use tag_id or cat.
Casting the Variable
Actually, this is one of those places where we probably want to cast the variable. We want to make sure that the variable is an integer. This is called casting the variable so what we’re doing is in front of the value we’re saying, “give it to us as an integer”.
The same thing is true here. We want to make sure that we’re getting this as an integer regardless of how it comes across because posts_to_show and either cat or tag_id require integers. So, we’re going to cast whatever this value turns out to be as an integer. It’s not going to be Cherries anymore that show up there but let’s go look at our page.
Well, that didn’t work. So, number of posts to show, category, 5, 3, if($tax1 === ‘post_tag’) then ‘tag_id’ else ‘cat’, $args1, $tax_name, $term1, ‘posts_per_page’ => $posts1.
We’re going to have to upload our wp-config file again with debug true, see what’s happening there. I must have a typo in there some place. “Fatal error: Call to undefined function int”. Okay, “php cast to int”. That’s right, I’m sorry it’s (int) like that not int with parenthesis around it. So it’s not int( like this, it’s (int) like this. That cast is a cast that has an (int) or has an integer.
This time we’ve got Green Apples showing up there so we’ve got the right one. We’re still getting that funny notice which will go away as soon as I set wp-config to false. Let’s do that.
However, notice that now we’re showing the Green Apples which was the category 5 rather than the Cherries which was 4. Let’s come back over to our Theme Settings and say 4 and save it and refresh this, now it’s Cherries.