[Plugin: Easy Digital Downloads] Using tags and categories
-
Hi
I need to describe my downloads with tags and/or categories. More precisely: to view them on my single pages, search results and checkout.
How can I do this?Would be great if CSS classes dependent on categories could be used, eg.
<div id="post-123" class="post-123 category-name download type-download status-publish hentry">
-
You can already do this, though it largely depends on your theme.
Are the the developer that built your theme, or are you comfortable modifying it?
What theme are you using?
Usually modifying Twentyeleven
If you are using Twenty Eleven, you should already have CSS classes added to the “body” tag for the post type. The category and tag might also be there, but I don’t think so.
See this page for info on adding new classes: http://codex.wordpress.org/Function_Reference/body_class
The taxonomy for tags/categories is “download_tag” and “download_category”.
Does that tell you what you need?
Hi mordauk
I’m not a programmer, and I find it hard to talk about PHP coding 🙁Twenty Eleven is very well equipped with CSS classes, and I can use them (adjusting the template), because they are already! 🙂
But..
How to modify the EDD templates to using classes of EDD taxonomy?How to display the current category and current tags (in footnotes of each item page, search results, and checkout) – for informational purposes, and as links to lists of other downloads (for tracking chosen category/tag)?
Please, is there a piece of code that gets and shows the current download taxonomy?
Just to make sure I have this right:
1. You want to add CSS class names to your template that contain the names of the current download tags/categories.
2. You want to show the tags/categories applied to the current download somewhere on the download details page.
Correct?
Yes, correct!
with a little clarifying:
…CSS class names, that contain the slugs of the current download tags/categories.Add this to your functions.php, it should do the trick:
function download_category_id_class($classes) { global $post; foreach((get_the_terms($post->ID), 'download_category') as $category) $classes[] = $category->slug; return $classes; } add_filter('post_class', 'download_category_id_class'); function download_tag_id_class($classes) { global $post; foreach((get_the_terms($post->ID), 'download_tag') as $tag) $classes[] = $tag->slug; return $classes; } add_filter('post_class', 'download_tag_id_class');
I like you wanted to figure out, but…
Parse error: syntax error, unexpected ‘,’ in /wp-content/themes/twentyten/functions.php on line:
foreach((get_the_terms($post->ID), 'download_category') as $category)
Whoops, it should be this:
function download_category_id_class($classes) { global $post; foreach(get_the_terms($post->ID), 'download_category') as $category) $classes[] = $category->slug; return $classes; } add_filter('post_class', 'download_category_id_class'); function download_tag_id_class($classes) { global $post; foreach(get_the_terms($post->ID), 'download_tag') as $tag) $classes[] = $tag->slug; return $classes; } add_filter('post_class', 'download_tag_id_class');
The same error, on the same line:
foreach(get_the_terms($post->ID), 'download_category') as $category)
Sorry, fixed:
function download_category_id_class($classes) { global $post; foreach( get_the_terms( $post->ID, 'download_category' ) as $category ) { $classes[] = $category->slug; } return $classes; } add_filter('post_class', 'download_category_id_class'); function download_tag_id_class($classes) { global $post; foreach( get_the_terms( $post->ID, 'download_tag' ) as $tag ) { $classes[] = $tag->slug; } return $classes; } add_filter('post_class', 'download_tag_id_class');
This goes in the right direction – EDD-from classes have been added
But… 🙂
On every post which has no tag or/and no category chosen WP echoes this:Warning: Invalid argument supplied for foreach() in /wp-content/themes/twentyten/functions.php
Pages have no tags and aren’t assigned to any category, so every page displays the double warning, and additionally prints classes:
Warning: Invalid argument supplied for foreach() in /wp-content/themes/twentyten/functions.php on line … (for category)
Warning: Invalid argument supplied for foreach() in /wp-content/themes/twentyten/functions.php on line … (for tag)
class=”post-123 page type-page status-publish hentry”>Maybe some conditional loop will help? else/if? I don’t know how to use it…
For standard post (not from EDD) there is double error, too.
Warning: Invalid argument supplied for foreach() in /wp-content/themes/twentyten/functions.php on line … (accordingly for category)
Warning: Invalid argument supplied for foreach() in /wp-content/themes/twentyten/functions.php on line … (accordingly for tags)
class=”post-1 post type-post status-publish format-standard hentry category-uncategorized tag-tag1″>This will fix it:
function download_category_id_class($classes) { global $post; $categories = get_the_terms( $post->ID, 'download_category' ); if( $categories ) { foreach( $categories as $category ) { $classes[] = $category->slug; } } return $classes; } add_filter('post_class', 'download_category_id_class'); function download_tag_id_class($classes) { global $post; $tags = get_the_terms( $post->ID, 'download_tag' ); if($tags) { foreach( $tags as $tag ) { $classes[] = $tag->slug; } } return $classes; } add_filter('post_class', 'download_tag_id_class');
You are GREAT! 🙂 Thank you!!!
- The topic ‘[Plugin: Easy Digital Downloads] Using tags and categories’ is closed to new replies.