I am using get_categories to list the child categories of a parent category.
I want to add an image to the child categories, using the get_categories output.
- I can either take the featured image from any of the posts that are children of the category that I am using get_categories on, IE the grand-child of the parent category. I do not want to show any other grand child information, and would want just one featured image from each set of category children.
The code I currently use is
$args = array('child_of' => 1 );
$categories = get_categories($args);
foreach($categories as $category) {
echo '<p>Category:'. $category->name.' </p> ';
echo '<p> Description:'. $category->description . '</p>';
echo PUT CAT IMAGE HERE;
}
- Or I could add an image to a category using some plugin, and show it in the
foreach($categories as $category)
output.
But what is the best (and easiest) way to implement this?
This is possible with a filter on
get_terms
.There are several queries in there, so add that filter only when you need it and remove the filter afterwards.
There are a number of related image functions that you could use instead, if
wp_get_attachment_image()
doesn’t work for your needs, and you can pass a$size
parameter towp_get_attachment_image()
— second parameter– to get different image sizes. For example replace the line of code with this:You can further alter the output of
wp_get_attachment_image()
by applying a filter towp_get_attachment_image_attributes
— for example, to add a class as done here.Instead of using
get_categories()
, I’d suggest you take a look atwp_list_categories()
. You can get the same output with it, but it has the benefit to be highly customizable.There are two ways for customization, either via the filter hook
wp_list_categories
– see in source – or by extending theWalker_Category
class – see in source. The latter gives you much more possibilities for customizing, but might be over the top for minor changes. So you have to evaluate what fits your needs best.Because this topic is actually pretty well covered on WPSE and SO, I’ll just give you a short list of references. Besides that you have not given a specific enough description of your needs, it remains a bit vague, although one can know what you want. Anyway, below resources should help you to chose the method fitting for you and additionally should enable you to achieve it.
wp_list_categories
Walker_Category
classOne more remark, personally I would almost always opt for the extending the
Walker_Category
class method, but for cases where the change you want is very minor. This is of course a bit of personal preference, but has some background, particularly because of re-usability, extendability and – even if it doesn’t necessary seem like it – after all it often – maybe even most of the time – is the easier way to achieve the custom output.Thumbnail from post in child category:
This way you can retrieve your “category thumbnail” from a random post belonging to one of the child categories.
I’ve commented the code to make it understandable for you how this is done, besides that it should be pretty much self-explanatory.
Usage like this:
In your code use it like shown below:
Category thumbnail via plugin:
This has the benefit of having actual category thumbnails.
I’ve given the plugin – Category Thumbnails – I mentioned in the comment above a quick test. It’s generally working and I feel it’s a better solution than what you’re proposing in your answer, because it enables you to manage the thumbnails via the wordpress media library.
I discovered a minor bug, the plugin author is notified and replied instantly, so the bug will be fixed soon. If you can’t wait you can fix it yourself like this:
Or you just use
get_the_category_thumbnail()
instead, you just have toecho
it:Another minor disappointment is that the plugin and functions it offers doesn’t give you the possibility to work with the generated image sizes. Which isn’t that good, because we want the correct size and not always the full size. I’m going to notify the author about that too, so he can improve his plugin further. In the meantime this luckily can easily be fixed by constructing a custom function:
Usage like this:
This should help you to get this working. Like I said, it’s preferable because you can manage your category thumbnail via the media library. In your code use it like shown below:
Well, I managed to do what I believe you want to do using Advanced Custom Fields, creating a new set of custom fields with one image custom field. From there, set the rules on location to “Taxonomy Term” > “is equal to” > “Categories” (or whatever taxonomy you were using this for).
Once you publish this, it should update where you edit the taxonomies with a new field for an image. Now, it’s all a matter of using the ACF function “get_field()” inside your category foreach loop. Here’s how you get field from the category.
Example:
If you used the default settings for ACF, you should get an array out of this, which is the image object, and for that, read up on Image Field Types from ACF.
It took me <5 minutes to set this up, and the plugin is free. I hope that’s quick and easy for you!
Best of luck!
WordPress has the code – http://codex.wordpress.org/Function_Reference/get_the_category
This allows a set up of images inside a get_categories functions. The images have to be setup to match the function. So if $category->name is used, the image must be saved in the right folder with the matching name of the category.