Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Functions and Global Variables Reply To: Functions and Global Variables

#203162
Steven
Participant

My mind a’sploded a little when I got to the code examples. I’m self-“taught” in PHP, so I’m still trying to grasp the basics… and the basics/best practices of programming in general.

Let me quickly try to explain exactly what I’m trying to do, so maybe you can expound a little better.

This is for a WordPress site. I created a custom post type, and created a number of custom fields for it via Advanced Custom Fields Pro plugin. There is a “State” field with four possible values: IA, MN, SD, WI, and four respective county fields: “Iowa County”, “Minnesota County”, etc. These four county fields are populated with the respective counties for that state. Now, in ACF I set it up that way, so I could tell it, if the user has selected X state then display the dropdown for Y county (it made sense to me at the time, maybe I’m wrong in that decision?).

Anyway… now when I’m in the WP Loop I need to grab their State value, and depending on what that is, grab the value of their corresponding county field. I started with if/else statements, did a switch statement, then tried moving that switch statement to my own custom_functions.php file (which is required inside functions.php) and placed it inside a function, thinking I was really clever… Then I could just call county($state) any time I needed to grab an entry’s county.

So my original function was a bad idea (thanks!), but I still can’t understand your examples (sorry..)