Forums

Taxonomy Images
combining logic of my function with tax images (9 posts)

  1. Anointed
    Member
    Posted 11 months ago #

    I am using a simple function below which basically grabs the image from a post along with the title and this is then used in one of my menu's.

    what I would like to do on this menu is keep the same logic, but if 'get_the_image()' returns null then grab the tax image assigned to the post category. That way if the article is category 'video', and there is no image in the post, then I can display the tax term for the category 'video'. Then I never have a line without an image in my logic, and it is better than my current method of assigning a generic global image.

    **Major bonus if the mod allows me to use this with any custom tax term, so if no 'category tax' then lets use post_tag (or one I define)

    <ol>
    
    						<?php
    						$navposts_query = new WP_Query( 'posts_per_page=5');
    
    							while ( $navposts_query->have_posts() ) : $navposts_query->the_post(); ?>
    
    						<li>
    							<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    								<img src="<?php if ( function_exists( 'get_the_image' ) ) get_the_image( array( 'return_url' => 'true', 'echo' => 'false', 'link_to_post' => 'false', 'default_image' => '/a/screen/default_image.jpg', 'image_scan' => 'true', 'meta_key' => 'feature_img', 'width' => '45', 'height' => '55' ) ); ?>" width="45" height="55">
    								<strong><?php the_title(); ?></strong>
    								<span class="meta">
    									<em>Author: <?php the_author_link() ?></em>
    									<em><?php the_time('F jS, Y') ?></em>
    								</span>
    							</a>
    						</li>
    
    						<?php endwhile; wp_reset_postdata(); ?>
    
    					</ol>

    thnx

  2. Michael Fields
    Theme Wrangler
    Posted 11 months ago #

    The API currently does not support this. I'll need to do some testing to see the best way to handle such an process. I think you are like the 3rd or so person to request this functionality so it's definitely in my mind to do/ I just don't have time at the moment to do all of the testing involved to get you the best possible solution. Feel free to hit me up in a few days if you are getting restless with this.

  3. pauldewouters
    Member
    Posted 11 months ago #

    Michael,

    how do I test if there is an image assigned to the taxonomy?
    thanks

  4. Michael Fields
    Theme Wrangler
    Posted 11 months ago #

    Images are not assigned directly to taxonomies.
    Images are associated with terms.

    Data for terms associations are stored in the options table of the database. Here's an easy way to see what the plugin has stored:

    function dump_taxonomy_images_plugin_data() {
    
    	$associations = get_option( 'taxonomy_image_plugin' );
    	print '<pre>' . print_r( $associations, true ) . '</pre>';
    
    	$settings = get_option( 'taxonomy_image_plugin_settings' );
    	print '<pre>' . print_r( $settings, true ) . '</pre>';
    
    }
    add_action( 'wp_head', 'dump_taxonomy_images_plugin_data' );

    The associations are stored in the "taxonomy_image_plugin" option as a numerically indexed array.

    The keys represent the term_taxonomy_id of the term.
    The value represents the image's ID.

    Each value is intended to be an integer and should be escaped as such before feed into a low-level function. Most higher-lever functions (like template tags) should require no sanitation. I always cast as int in these cases anyway ... it's easy enough to do :)

  5. Michael Fields
    Theme Wrangler
    Posted 11 months ago #

    BTW ... There is a ton of documentation in the source of this project. But I am coming to realize that further documentation may be necessary. If you guys could help out with this that would be great!

    I'm not really looking for you to write it, rather point out areas that could be improved. Just by reading this it seems like there should be a "How this plugin works" page that explains what we have talked about here. Any ideas for other such pages?

    If so, please use the issue tracker:
    https://github.com/mfields/Taxonomy-Images/issues

    Thanks!

  6. Anointed
    Member
    Posted 11 months ago #

    Have to admit that your docs are always the most explicit I come across. Very easy to understand and well thought out. I only wish others did this level of documentation with code.

    I only seem to run into problems when trying to do more 'complex' things with code snippets. That is not a doc problem, but more my still learning php and best practices. Even worse is when I try to make a function do something that it explicitly says it doesn't do LOL.

    Best solution then is to buy tacos.

  7. Anointed
    Member
    Posted 11 months ago #

    Your probably still pretty busy, but did want to drop a line letting you know that I am still quite interested in doing this if possible.

  8. Michael Fields
    Theme Wrangler
    Posted 11 months ago #

    This is one way of doing this. It should work without either plugin installed:

    <?php
    while( have_posts() ) {
    
    	the_post();
    
    	$src = 'http://full-path.to/default-image.jpg';
    
    	$post_tags = apply_filters( 'taxonomy-images-get-the-terms', array(), array( 'taxonomy' => 'post_tag' ) );
    
    	if ( isset( $post_tags[0]->image_id ) ) {
    		$img = wp_get_attachment_image_src( $post_tags[0]->image_id, 'thumbnail' );
    		if ( isset( $img[0] ) ) {
    			$src = $img[0];
    		}
    	}
    
    	if ( function_exists( 'get_the_image' ) ) {
    		$src = get_the_image( array(
    			'return_url'    => 'true',
    			'echo'          => 'false',
    			'link_to_post'  => 'false',
    			'default_image' => $src,
    			'image_scan'    => 'true',
    			'meta_key'      => 'feature_img',
    			'width'         => '45',
    			'height'        => '55',
    			'echo'          => false
    		) );
    	}
    
    	else if ( has_post_thumbnail() ) {
    	   $img = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'thumbnail' );
    	   if ( isset( $img[0] ) ) {
    			$src = $img[0];
    		}
    	}
    
    }
    ?>
  9. Anointed
    Member
    Posted 11 months ago #

    Absolutely fantastic!
    Will play around with it a bit tomorrow, otherwise I end up pulling another all nighter.

    I smell tacos.

    thnx

Reply

You must log in to post.

About this Plugin

About this Topic