Support » Plugin: WPCustom Category Image » Call category image from a function

  • Hi,

    I found out about your plugin from a tutorial. I’m trying to install it on a theme functions file but it doesn’t seem to be working, maybe you can help. What the theme does is it loads an image with the caption “no image” if it isn’t found. See the function here:

    function get_thumbnail($postid=0, $size='full') {
    	if ($postid<1)
    	$postid = get_the_ID();
    	$thumb_key = get_theme_mod('thumb_key');
    	if($thumb_key)
    		$thumb_key = $thumb_key;
    	else
    		$thumb_key = 'thumb';
    	$thumb = get_post_meta($postid, $thumb_key, TRUE); // Declare the custom field for the image
    	if ($thumb != null or $thumb != '') {
    		return $thumb;
    	} elseif ($images = get_children(array(
    		'post_parent' => $postid,
    		'post_type' => 'attachment',
    		'numberposts' => '1',
    		'post_mime_type' => 'image', ))) {
    		foreach($images as $image) {
    			$thumbnail=wp_get_attachment_image_src($image->ID, $size);
    			return $thumbnail[0];
    		}
    	} else {
        	return get_bloginfo ( 'stylesheet_directory' ).'/images/default_thumb.gif'
    	}
    
    }

    So I changed the final else statement to look like this:

    function get_thumbnail($postid=0, $size='full') {
    	if ($postid<1)
    	$postid = get_the_ID();
    	$thumb_key = get_theme_mod('thumb_key');
    	if($thumb_key)
    		$thumb_key = $thumb_key;
    	else
    		$thumb_key = 'thumb';
    	$thumb = get_post_meta($postid, $thumb_key, TRUE); // Declare the custom field for the image
    	if ($thumb != null or $thumb != '') {
    		return $thumb;
    	} elseif ($images = get_children(array(
    		'post_parent' => $postid,
    		'post_type' => 'attachment',
    		'numberposts' => '1',
    		'post_mime_type' => 'image', ))) {
    		foreach($images as $image) {
    			$thumbnail=wp_get_attachment_image_src($image->ID, $size);
    			return $thumbnail[0];
    		}
    	} else {
            // get the categories assigned to the post
            $post_cats = get_the_category( $postid );
            // no categories, unlikely but can't do anything
            if ( count( $post_cats ) == 0 ) return get_bloginfo ( 'stylesheet_directory' ).'/images/default_thumb.gif';
            // we'll just take the first
            $term_id = $post_cats[0]->term_id;
            // using function from plugin, get the id of the category image
            $attach_id = WPCustomCategoryImage::get_attachment_id( $term_id );
            // no category image, out of here
            if ( !$attach_id ) return get_bloginfo ( 'stylesheet_directory' ).'/images/default_thumb.gif';
            // now store this in the post's metadata using id _thumbnail_id
            return $attach_id;
        }
    }

    …but it doesn’t seem to be working. It shows a broken image.

    Any ideas?
    Thanks.

    http://wordpress.org/plugins/wpcustom-category-image/

  • The topic ‘Call category image from a function’ is closed to new replies.