• In one of my menus I use the standard list categories function below. What would be the best way to combine your tax images with this so that it will show image -> tax term on each li ?

    *sorry I’m being so lazy and not digging through the code to force it to work. Just to many projects running through my head right now.

    thnx

    <?php
    
    						$taxonomy     = 'post_tag';
    						$orderby      = 'name';
    						$show_count   = 0;      // 1 for yes, 0 for no
    						$pad_counts   = 0;      // 1 for yes, 0 for no
    						$hierarchical = 1;      // 1 for yes, 0 for no
    						$title        = '';
    						$number		  = '100';
    						$hide_empty   = '1';
    
    						$args = array(
      							'orderby'      => $orderby,
      							'show_count'   => $show_count,
      							'pad_counts'   => $pad_counts,
      							'hierarchical' => $hierarchical,
      							'title_li'     => $title,
      							'number'	   => $number,
      							'hide_empty'   => $hide_empty
    						);
    					?>
    
    					<ol>
    					<?php wp_list_categories( $args ); ?>
    					</ol>

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Contributor Michael Fields

    (@mfields)

    Something like this works:

    <?php
    $terms = apply_filters( 'taxonomy-images-get-terms', '', array(
    	'taxonomy' => 'category',
    	) );
    if ( ! empty( $terms ) ) {
    	print "\n" . '<ul>';
    	foreach( (array) $terms as $term ) {
    		print "\n" . '<li>';
    		print "\n\t" . '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' ) . '</a>';
    		print "\n\t" . '<span class="category-name">' . esc_html( $term->name ) . '</span>';
    		print "\n" . '</li>';
    	}
    	print "\n" . '</ul>';
    }
    ?>
    Thread Starter Anointed

    (@anointed)

    thnx Michael. This is gonna open a few new possibilities that I have been mulling over for years now. I also saw the response to the other query I posted. No rush, I have lots of areas that I am playing around with.

    Hello guys !

    First, thanks a lot Michael for providing such a useful plugin. So far it’s working like a charm. I used the aforementioned function on a taxonomy archive, and it’s working great !

    However, it only displays the terms which actually *have* an image associated with them, despite the fact I’d like to display the terms which haven’t, too (it would be a great plus to be able to display a default image, in that case).

    Ideal scenario would be :

    1. if the term has an image attached, display the thumbnail size ;
    2. if the term hasn’t an image attached, display a default image.

    I’ve tried the following code, so far with no success:

    <?php
    $terms = apply_filters( 'taxonomy-images-get-terms', '', array(
    	'taxonomy' => $post->post_name,
    	) );
    $image_url = apply_filters( 'taxonomy-images-queried-term-image-url', '', array('image_size' => 'full') ); 
    
    if ( ! empty( $terms ) ) {
    	print "\n" . '<ul>';
    	foreach( (array) $terms as $term ) {
    		print "\n" . '<li>';
    		if ( ! empty ($image_url) ) {
    			print "\n\t" . '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '"><img src=' . $image_url . '</a>';
    			print "\n\t" . '<span class="category-name">' . esc_html( $term->name ) . '</span>';
    		} else {
    			print "\n\t" . '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '"><span class="category-name">' . esc_html( $term->name ) . '</span></a>';
    		}
    		print "\n" . '</li>';
    	}
    	print "\n" . '</ul>';
    }
    ?>

    NB : $post->post_name allows me to dynamically retrieve the taxonomy name.

    Any help would be much appreciated ! ?? Many thanks in advance.

    Plugin Contributor Michael Fields

    (@mfields)

    Have you tried something like this:

    <?php
    $terms = apply_filters( 'taxonomy-images-get-terms', '', array(
    	'taxonomy' => $post->post_name,
    ) );
    
    if ( ! empty( $terms ) ) {
    	print "\n" . '<ul>';
    	foreach( (array) $terms as $term ) {
    		print "\n" . '<li>';
    		if ( ! empty ( $term->image_id ) ) {
    			print "\n\t" . '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' );
    			print "\n\t" . '<span class="category-name">' . esc_html( $term->name ) . '</span>';
    		}
    		else {
    			print "\n\t" . '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '"><img src="default.png">';
    			print "\n\t" . '<span class="category-name">' . esc_html( $term->name ) . '</span>';
    		}
    		print "\n" . '</li>';
    	}
    	print "\n" . '</ul>';
    }
    ?>

    Thanks a lot ! ??

    Unfortunately it doesn’t change anything : only the terms which already have an image attached are showing up. The other terms don’t, as though the else statement wasn’t working.

    Edit : wait, I’ve tried on another taxonomy archive page, and it’s working ! I will compare the taxonomy settings to find what’s the problem on my first taxonomy. I’ll keep you updated.

    Plugin Contributor Michael Fields

    (@mfields)

    Please let me know exactly what you are trying to do. using get_terms() on a term archive page seems a bit strange to me. I might be able to help.

    Here we go :

    Let’s say I have one cutom post type called “Movies”, and one custom taxonomy called “Directors” associated to this custom post type.

    When I browse https://www.mydomain.com/movies/, I do have an archive page with all my custom posts filed under “Movies”. However, when I browse https://www.mydomain.com/directors/, I get a 404 page since taxonomies don’t have an automatic archive page url.

    That’s why I create the “Directors” page on which I use a custom template called “taxonomy-tagcloud.php” (whatever the name).

    My goals are :

    1. Display the Taxonomy name as h1 ;
    2. Display a short description (= the Page content) ;
    3. Then, display the list of terms under the “Directors” taxonomy :
    1. if the term has an image attached : display it ;
    2. if the term doesn’t have an image attached, display a default image.

    That’s all, basically.

    Here is the complete taxonomy-tagcloud.php :

    <?php
    /**
     * @package WordPress
     * @subpackage Toolbox
     */
    
    get_header(); ?>
    
    		<div id="primary">
    			<div id="content" role="main">
    
    				<?php // here, the page title and content ?>
    				<?php the_post(); ?>
    				<?php get_template_part( 'content', 'page' ); ?>
    
    				<?php //here, the code to display all terms with their own image, otherwise with the default image ?>
    <?php
    // I use $post->post_name to dynamically get the page's slug, i.e "directors"
    $terms = apply_filters( 'taxonomy-images-get-terms', '', array(
    	'taxonomy' => $post->post_name,
    ) );
    
    if ( ! empty( $terms ) ) {
    	print "\n" . '<ul>';
    	foreach( (array) $terms as $term ) {
    		print "\n" . '<li>';
    		if ( ! empty ( $term->image_id ) ) {
    			print "\n\t" . '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' );
    			print "\n\t" . '<span class="category-name">' . esc_html( $term->name ) . '</span>';
    		}
    		else {
    			print "\n\t" . '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '"><img src="https://dummyimage.com/150/000/fff.png">';
    			print "\n\t" . '<span class="category-name">' . esc_html( $term->name ) . '</span>';
    		}
    		print "\n" . '</li>';
    	}
    	print "\n" . '</ul>';
    }
    ?>
    
    			</div><!-- #content -->
    		</div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Now, I made some other tests, and here is the problem I get with this code :

    • if the taxonomy contains only one term, and if this only term doesn’t have any image attached, then the code works : I get the default image and the term link ;
    • if the taxonomy contains many terms, and if at least one of these terms does have an image attached, then the terms without any image attached don’t show.

    Do you have any idea about what the problem could be ?

    PS : of course, I’m open to any other solution. Maybe using a page to achieve this isn’t the right way ? I’m close to get it working, though.

    Plugin Contributor Michael Fields

    (@mfields)

    Thanks for the explanation!
    That explains why you are using $post->post_name as the taxonomy name. I couldn’t figure that out for the life of me ??

    Have you tried setting the having_images argument? This is most likely what you need:

    $terms = apply_filters( 'taxonomy-images-get-terms', '', array(
        'taxonomy'      => $post->post_name,
        'having_images' => false,
    ) );

    Maybe using a page to achieve this isn’t the right way ? I’m close to get it working, though.

    IMHO it’s the best way to do it. Sure, there might be more complicated solutions, but this one is the best we have at the moment.

    Thank you so much ! It works perfectly \m/

    @kreestal
    @michael Fields

    Thank you so much for these posts. I’ve been working with a very similar situation — a sermon archive for a church website with taxonomy images. I used kReEsTaL’s method of creating a page with the custom template. This saved me a pile of time and headaches. ??

    You’re very welcome, d.keeling! I’m happy my solution was useful to you ??

    hello, since i have a little php knowledge;

    i think the answer i am searching for is here but do not know how to combine (even not sure if that is possible..)

    anyway, i am listing the categories in a page template.

    as simple as <ol><?php wp_list_categories('title_li=&child_of=1'); ?></ol>

    and did not edit the “archive.php” as said in the plugin notes. (because i do not want the images displayed there..)

    and want to list those categories not as text links, instead, i’d like them to be listed as images (in full size)

    so where should i put this code?? (which i think i should use ?? )

    i think the answer is this ??

    link here does not show up so i copy micheal fields first answer here

    <?php
    $terms = apply_filters( 'taxonomy-images-get-terms', '', array(
    	'taxonomy' => 'category',
    	) );
    if ( ! empty( $terms ) ) {
    	print "\n" . '<ul>';
    	foreach( (array) $terms as $term ) {
    		print "\n" . '<li>';
    		print "\n\t" . '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' ) . '</a>';
    		print "\n\t" . '<span class="category-name">' . esc_html( $term->name ) . '</span>';
    		print "\n" . '</li>';
    	}
    	print "\n" . '</ul>';
    }
    ?>

    or an exact code would be highly appreciated :/

    thanks..

    an update: wow!

    did not think that could be even simplier..

    anyways, i just copied the above code, and everything works now!!!
    thanks anyway ??

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘tax images plus wp_list_categories’ is closed to new replies.