I really need a comprehensive solution to this, so I’m offering up almost a quarter of my rep in bounty 🙂
I would like to have a plugin that creates a custom category listing on my home page. To do this, I’d like the “Category Edit” screen to be augmented with some additional functions as described below…
- Adds “Upload/Delete Image” handler to the Category Edit Screen for a Given Category (this allows the end user to upload an image that will be used to represent the category. Ideally, it should automatically be resized to 125 pixels wide by the plugin when uploaded.
- Adds “Category Title” input field to the Category Edit Screen for a Given Category. This is in addition to the existing default “Name” field.
- Adds a checkbox titled “Show on homepage listing” to allow selective display of each category on the home page listing.
- When the home page is viewed, the plugin appends the_content with the custom category listing, including each category’s custom thumbnail and title.
The resulting markup should be an unordered list with no nesting, like so…
<ul class="custom-categories">
<li>
<span><a href="link-to-category-1"><img src="the-category-1-image" /></a></span>
<a href="link-to-category-1">Category 1 Title</a> The category description text goes here
</li>
<li>
<span><a href="link-to-category-2"><img src="the-category-2-image" /></a></span>
<a href="link-to-category-2">Category 2 Title</a> The category description text goes here
</li>
etc...
</ul>
Here’s a stub file I’ve started…
add_filter( 'the_content', 'cb_category_listing' );
function cb_category_listing( $content )
{
if ( is_home() ) {
$cat_args['title_li'] = '';
$cat_args['exclude_tree'] = 1;
$cat_args['exclude'] = 1;
$myContent = wp_list_categories(apply_filters('widget_categories_args', $cat_args));
$content .= $myContent;
}
return $content;
}
Note: This is for older versions of WP < 3.9 prior to the new media upload being introduced
Here’s how to add fields and save the values on the category edit screen as well as a method of adding an image upload field.
Adding fields to category edit screen
To start we need to get some code showing up on the category edit screen.
The simplest way to store our custom values is in the options table (there should be a taxonomy-meta table really but never mind). This way we only need to make one query to get the meta data for all of our categories. If anyone has a better idea for storage then speak up!
For a checkbox you’re simply storing true or false so you’d use
category_extras[$tag->term_id][show_on_home]
for the name attribute and use the value stored in$category_meta
to determine if it’s checked or not.You may want to add some extra processing or sanitisation to the save function – mine is just a quick n dirty example.
The image field
This is a fair bit of code and quite complicated so I won’t explain it all here but the comments describe the purpose of each function. We can discuss in the comments if you want to.
The following functions add a link to the category edit screen which brings up the wordpress media library/image upload popup. You can then upload a picture and click to use it. You’ll then have the image ID and thumbnail url available to you with the other meta above.
Accessing the information
In your
my_function
bit taken from your own answer you would then access the metadata like so:The easy way would be with something like this http://wordpress.org/extend/plugins/category-images/ . Or you could write your own hook.
Here was my first attempt…
Obviously, this simply creates a listing of category names with a link to each category. So it gets me started. However, I still need…
A means to associate a category thumbnail image with each category, and show it on the category listing in my code above.
A means to include a custom category title, apart from the category name, and show it on the category listing (in place of the “Category Name”)
A means to show/hide each category from the listing. Ideally, with a checkbox “Show on homepage listing” that can be edited on the category’s edit screen.
Here’s my updated code. Getting there, but still haven’t tackled the thumbnail image or adding the custom fields (thumbnail image, custom title, show/hide) to the category editor admin…