WordPress.org

Ready to get started?Download WordPress

Forums

Easy Digital Downloads
[resolved] Using tags and categories (23 posts)

  1. Ayek
    Member
    Posted 2 years ago #

    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">

    http://wordpress.org/extend/plugins/easy-digital-downloads/

  2. Pippin Williamson
    Pippin's Plugins and Plugin Reviewer
    Plugin Author

    Posted 2 years ago #

    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?

  3. Ayek
    Member
    Posted 2 years ago #

    Usually modifying Twentyeleven

  4. Pippin Williamson
    Pippin's Plugins and Plugin Reviewer
    Plugin Author

    Posted 2 years ago #

    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?

  5. Ayek
    Member
    Posted 2 years ago #

    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?

  6. Pippin Williamson
    Pippin's Plugins and Plugin Reviewer
    Plugin Author

    Posted 2 years ago #

    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?

  7. Ayek
    Member
    Posted 2 years ago #

    Yes, correct!

    with a little clarifying:
    ...CSS class names, that contain the slugs of the current download tags/categories.

  8. Pippin Williamson
    Pippin's Plugins and Plugin Reviewer
    Plugin Author

    Posted 2 years ago #

    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');
  9. Ayek
    Member
    Posted 2 years ago #

    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)

  10. Pippin Williamson
    Pippin's Plugins and Plugin Reviewer
    Plugin Author

    Posted 2 years ago #

    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');
  11. Ayek
    Member
    Posted 2 years ago #

    The same error, on the same line:
    foreach(get_the_terms($post->ID), 'download_category') as $category)

  12. Pippin Williamson
    Pippin's Plugins and Plugin Reviewer
    Plugin Author

    Posted 2 years ago #

    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');
  13. Ayek
    Member
    Posted 2 years ago #

    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...

  14. Ayek
    Member
    Posted 2 years ago #

    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">

  15. Pippin Williamson
    Pippin's Plugins and Plugin Reviewer
    Plugin Author

    Posted 2 years ago #

    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');
  16. Ayek
    Member
    Posted 2 years ago #

    You are GREAT! :) Thank you!!!

  17. Ayek
    Member
    Posted 2 years ago #

    I only miss the last piece of code to be completely happy, please...
    How to display the information about categories and tags in the post meta?

    Posted in EDD Category 1, EDD Category 2 | Tagged EDD Tag 1, EDD Tag 2

  18. Pippin Williamson
    Pippin's Plugins and Plugin Reviewer
    Plugin Author

    Posted 2 years ago #

    You will use this function:

    <?php echo get_the_term_list( $post->ID, 'download_tag', 'Tagged: ', ', ', '' ); ?>

    and

    <?php echo get_the_term_list( $post->ID, 'download_category', 'Posted in: ', ', ', '' ); ?>

  19. Ayek
    Member
    Posted 2 years ago #

    Splendid! :) Lot of thanks and best regards!

  20. acasperw
    Member
    Posted 1 year ago #

    Is there a way of getting this to work on the <div class="edd_download"> part? so each download is tagged by catagory

  21. @acasperw I'm not sure what you mean. Are you referring to a particular theme?

  22. acasperw
    Member
    Posted 1 year ago #

    Hey Pippin,
    Sorry i wasnt very clear. No not a theme, just a way of getting the Catagories and Tags to show as classes in the `<div class="edd_download">
    part: so <div class="edd_download Cat_1 Cat_2 Tag_1">

    Iv managed to frankenhack this by using

    <?php
    $cat_terms_as_text = strip_tags( get_the_term_list( $download->ID, 'download_category', '', ' ', '' ) );
    $af_class_cat = str_replace(" ", "_", $cat_terms_as_text);
    
    $tag_terms_as_text = strip_tags( get_the_term_list( $download->ID, 'download_tag', '', ' ', '' ) );
    $af_class_tag = str_replace(" ", "_", $tag_terms_as_text);
    ?>
    
    <div class="edd_download_inner <?php echo "af_cat_$af_class_cat $af_class_tag"; ?>">

    Inside the Shortcodes.php (yeah i know i have to re-update it everytime you release an update) but we both know my PHP + DB knowledge isnt very strong.

  23. Using post_class() would be a much better approach.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic