• Resolved DesignLoud

    (@designloud)


    Hi all, I am using the taxonomy meta found on github and I am having success until it comes to pulling in my images (source) In short I am having trouble getting the source of the image to print to use in my if statement found below. Everything else works great except for that. Am I missing something? I have tried wp_get_attachment_image_src, wp_get_attachment_image and almost everything else I can think of. It has to be a problem with my $category_id variable but I dont understand why that is if it works for the videos??

    <?php
    
    	$args = array( 'taxonomy' => 'properties_community' );  //pull in our taxonomy
    
    	$terms = get_terms('properties_community', $args);  //we need the terms to make sure each image/video matches the appropriate term or category being displayed
    
    	$category_id = $wp_query->get_queried_object_id($terms);
    
    	$meta = get_option('additional');
    	if (empty($meta)) $meta = array();
    	if (!is_array($meta)) $meta = (array) $meta;
    	$meta = isset($meta[$category_id]) ? $meta[$category_id] : array();
    	$images = $meta['community-image'];
    	$videos = $meta['youtube-video-id'];
    	$desc_comm = $meta['community-description']; ?>
    
        <div class="eight columns">
        <?php
    	if ($videos) {
    
    		//if videos then use them
    
    		echo '<iframe width="560" height="315" src="http://www.youtube.com/embed/' . $videos .'" frameborder="0" allowfullscreen></iframe>';
    
    	} elseif ($images) {
    
    		//if images use that
    
    	// get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
    	$src = wp_get_attachment_image_src($category_id,'thumbnail');
    	$src = $src[0];
    	// show image
    
    	echo '<img class="twelve columns" src="'.$src.'" />';
    
    	} else {
    
    		//otherwise lets just place a default image
    
    	} // end if images/videos/else
    	?>

    Any takers? 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter DesignLoud

    (@designloud)

    btw I am using this in my-taxonomy-archive.php

    Moderator bcworkz

    (@bcworkz)

    $wp_query->get_queried_object_id() does not accept parameters, so it is returning the ID of the currently queried object, which is apparently not the image attachment. The reason it works for videos is the video ID is stored under this ID, even though it may be incorrect. So when you retrieve the video ID using the same incorrect ID, it all works out.

    Since getting the correct image src requires using the correct ID, it is failing. I don’t know what the current query is, but if it’s the image attachment’s parent, you can use get_posts() passing the $category_id as the post_parent argument and post_type set to ‘attachment’ to get all attachments. From those you can determine the correct ID and then use wp_get_attachment_image_src() to get the src url.

    Thread Starter DesignLoud

    (@designloud)

    Pretty much I have it set that in my taxonomy-archive-template.php it at the top of the page it will show the current category name and then show the image/video that was uploaded to that category with the taxonomy-meta plugin mentioned above. Under this area it will display all the posts in that category like normal. The major thing here is that I want to pull the image or video that is uploaded to that category in my taxonomy. Video works (like you said probably by accident) but cant get the image.

    Moderator bcworkz

    (@bcworkz)

    Unfortunately, I’m unfamiliar with the tax-meta plugin and its process of uploading images to a category. If an attachment post type is created at the time, there will some way of querying for it and getting the src url that way. If not an attachment, the relationship to the category must exist somehow, you need to determine what that is and leverage it somehow into an image src url. For instance, what is stored in $meta['community-image']? Can that value lead us to the src url somehow?

    If you are having trouble figuring that all out, post a link to this plugin and I’ll see if I can make something out of it.

    Thread Starter DesignLoud

    (@designloud)

    sure, here is the gist: https://github.com/rilwis/taxonomy-meta and here is the documentation (which shows how to pull the images but only using a foreach)http://www.deluxeblogtips.com/taxonomy-meta-script-for-wordpress/

    I am going to continue working with it to see if I can get it with some fresh eyes but if you see a solution first I would appreciate the help.

    Thanks so much

    Just in case here is the paste of my template file: http://pastebin.com/3dn2VRUZ

    Moderator bcworkz

    (@bcworkz)

    OK, I have a better idea, but I’m still a bit confused because I’m unfamiliar with how your taxonomy corresponds to the array keys. It’s clear the images are associated with attachments, so if all else fails, you could create a new WP_Query using tax_query to get the right attachment ID and then get the src url with wp_get_attachment_image_src().

    Short of that, there’s a fair chance $images contains an array of attachment IDs. Before getting fancy with more queries, try something like wp_get_attachment_image_src($images[0],'thumbnail');

    Actually, it’s clear the attachment IDs are in $meta somewhere, so querying the attachments should be unnecessary, I just can’t be sure which key to use to get the IDs. ‘community-image’ seemed like as good a guess as any. I hope this is of some help.

    Thread Starter DesignLoud

    (@designloud)

    Ahh, finally got it, thanks so much for your help (again) 😀

    Moderator bcworkz

    (@bcworkz)

    You are welcome, I’m glad my confused ramblings made some sort of sense.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Returning attachment image source’ is closed to new replies.