• I need a shortcode for this plugin and found that it didn’t exist. Here’s my stab at it, perhaps someone can improve upon my code, but at least it’s a good first effort (as far as I can tell, it’s working on my site.) Open to feedback…I’m not the developer, just a user.

    I’ve added a fallback for posts without a category and/or without a category image, to use category ID 1 (uncategorized) – certain sites might need to change this value to something else.

    // CREATE SHORTCODE FOR "CATEGORIES IMAGES" OUTPUT 
    
    add_shortcode( 'category-image', 'categories_images' );
    
    function categories_images()
    {
        $category = get_the_terms( $id, 'category' );
        $categoryid = $category[0]->term_id;
        $taxonomy_image_url = get_option('z_taxonomy_image'.$categoryid);
        if(empty($taxonomy_image_url)) {
                // If no category, or if category doesn't have an image, use the image for "Uncategorized"
                $categoryid = 1; 
                $taxonomy_image_url = get_option('z_taxonomy_image'.$categoryid);
            }
        if(!empty($taxonomy_image_url)) {
            $attachment_id = z_get_attachment_id_by_url($taxonomy_image_url);
                if(!empty($attachment_id))
                    $category_image = wp_get_attachment_image($attachment_id, $size, FALSE, $attr);
                else {
                    $image_attr = '';
                    if(is_array($attr)) {
                        if(!empty($attr['class']))
                            $image_attr .= ' class="'.$attr['class'].'" ';
                        if(!empty($attr['alt']))
                            $image_attr .= ' alt="'.$attr['alt'].'" ';
                        if(!empty($attr['width']))
                            $image_attr .= ' width="'.$attr['width'].'" ';
                        if(!empty($attr['height']))
                            $image_attr .= ' height="'.$attr['height'].'" ';
                        if(!empty($attr['title']))
                            $image_attr .= ' title="'.$attr['title'].'" ';
                    }
                    $category_image = '<img src="'.$taxonomy_image_url.'" '.$image_attr.'/>';
                }
        }
        return $category_image;
    }
  • The topic ‘Shortcode’ is closed to new replies.