I’d like to add custom fields to a certain category.
A category only has the following fields:
Name:
Slug:
Parent:
Description:
Since I have a TV Series Site, I want to add some more fields, I want something like this, when I create a new category (Category=Series)
Name:
Artist:
Year:
Type:
Genre:
Summary:
Slug:
Parent:
Description:
And so on…
Any help please?
Thanks in advance.
I posted an How To about it a week ago http://en.bainternet.info/2011/wordpress-category-extra-fields
hope this helps.
Ohad.
Here are the details of the post:
The first thing we need to do is add the extra fields to the category edit form using the hook edit_category_form_fields and we use a simple function that will print out the extra fields.
As you can see i added 4 new fields and all of them are in an array Cat_meta[key] because that way we only create on row in the options table to save all of the category’s extra fields instead of a row for each field.
Next we need to save the extra fields in to the database once a user submits the category edit form and we do that using “edited_category” with a function that will run through each of the submitted fields and insert them to the database using the update_option function, like this:
From the code above you can see that all of the extra fields we’ve added are stored in the database’s options table with the name ‘category_ID’ , where ID is the id of the specific category we just edited and that means we can call this data in our plugins or theme files easily using the get_option function.
say for example my category id is 25 then my code will look like
As I stated in the beginning, I need to display a different image for each category, so in that case I added these few lines of code to my theme’s category.php right after the code that displays the category title:
Nice and easy and we are all done. The result should look similar to this:
As of WordPress 4.4, the add_term_meta(), the update_term_meta() and get_term_meta() functions have been added. This means that the code as provided by MxmastaMills can be updated to use a far less hacky approach.
Here is my update of it. There is only one field as I wanted to add a custom title, but it’ll work the same for all the fields you want to add.
This code works:
Paul Menard provided an example of how to create and use term meta in his blog…
Custom meta for new taxonomies in WordPress 3.0.
There’s no example of creating the DB table or checking
$_POST
vars are set, so you’ll need to do those little things yourself, but it looks like a decent code base to build on top of … 🙂