A website owner may want to display a certain set of Sub Level Categories associated with a member. To make it as easy as possible to do this, while offering flexible functionality around this, we created the getMemberSubCategory() function.
What getMemberSubCategory() Does
This function accepts a number of parameters that tell the software which Sub Categories to display, and how they should display in contexts that are compatible with the function.
For example, if I wanted to list the first 5 sub categories associated with each member in search results, using this function will make that happen. If I want to display the last Sub Level Category associated with a member under their name on their profile page, this function will also make that happen. If I want to add all of a member's Sub Level Categories to a Meta Description field for SEO purposes, this function will allow for that as well.
How It Works
The getMemberSubCategory() function accepts the following 5 arguments, in this order:
The ID of the member. The variable to use for this first parameter depends on the context that the function will be used in:
Members Search Results Page = $user_data['user_id']
Member Profile Page = $user['user_id']
Post Details Page & Post Search Results Page = $post['user_id']
Group Search Results (posts with multiple images) = $p['user_id']
Group Details Page (posts with multiple images) = $group['user_id']
If you want to explicitly specify an ID here, it will accept an integer, or simply use the corresponding variable above to capture the ID of the member in the context in which this variable is called. In other words, using the $user['user_id'] variable on a member's profile page will capture the ID of whichever member's profile page is being viewed.
The second argument will accept one the following values:
- "sub" = the scope will be limited to Sub Level Categories only, not Sub-Sub Level Categories.
- "subsub" = the scope will be limited to Sub-Sub Level categories only, and will skip any Sub Level Categories.
- "all" = the scope will be both Sub and Sub-Sub Level Categories.
The third argument will accept one of the following values:
- "first" = the function will first return the category with the lowest ID number and all subsequent categories will be returned in order of ID, ascending.
- "last" = the function will first return the category with the high ID number and all subsequent categories will be returned in order of ID, descending.
- "random" = the function will return random categories in a random order
The fourth argument will accept one of the following values:
- Any whole number (INT such as 1, 2, or 3)
- "all" = all categories associated with the member, regardless of number
The fifth argument will accept one of the following values:
- "text" = the categories will be returned in plain text format, as a comma-separated list (EG: Doctor, Lawyer, Fireman)
- "linked" = the categories will be returned in plain text format, as a comma-separated list, but each category will be wrapped in an HTML "a" tag, linking to the filename of the category (EG: <a href="/doctor">Doctor</a>, <a href="/lawyer">Lawyer</a>, <a href="/fireman">Fireman</a>)
- "array" = the categories will be return as an associative array, with the name of the category as the Key and the filename of the category as the Value (EG: ['Doctor'=>'/doctor', 'Lawyer'=>'/lawyer', 'Fireman'=>'/fireman])
Let's say we have a member with the following Sub and Sub-Sub Level Categories:
- Angel Food
- Ice Cream
- Jelly Bean
We add the following code to a widget on the profile page of a member:
echo getMemberSubCategory($user[user_id], "sub", "first", "all", "text");
Then the following would be printed on the page:
Cake, Ice Cream, Candy
As another example, let's say we added the following code instead:
$subCategoryArray = getMemberSubCategory($user[user_id], "subsub", "last", 3, "array");
Then $subCategoryArray would store the following value:
"Gummy" => "/gummy",
"Jelly Bean" => "/jelly-bean",
"Taffy" => "/taffy"
Important Note: The categories will appear in the Sort Order specified in the Member Categories page in the Admin:
Default Category Sort Order