WordPress.org

Ready to get started?Download WordPress

Forums

Taxonomy Images
Trouble incorporating taxonomy images into existing code (6 posts)

  1. brettsmason
    Member
    Posted 2 years ago #

    I'm currently trying to list all child categories on a parent level category, with a depth of 1, and the same as you go down a level, until you get to the final sub category in the chain.

    I have this code working, but I desperately need to get the images I have set using your (awesome!) plugin. I'm pretty new to php so am struggling some what - can anyone give any help in how to alter this to achieve my desired result?

    <?php global $ancestor; // Declare global ancestor variable ?>
    <?php foreach ($categories as $cat) {
    	if ($cat->cat_ID != 1) { // If category is not uncategorized ...
    	if (cat_is_ancestor_of($ancestor, intval($cat->cat_ID)) == false) { // .. and if the previous category in the loop wasn't the ancestor of this one ... ?>
    		<h2><a href="<?php echo get_category_link($cat->cat_ID); ?>"><?php echo ($cat->cat_name); ?></a></h2>
    		<?php echo ( category_description($cat->cat_ID) );  ?>
    		<?php print apply_filters( 'taxonomy-images-queried-term-image', '' ); ?>
    		<?php
    		$ancestor = intval($cat->cat_ID); }; // make this category the new ancestor
    		};
    	};
    ?>
  2. Michael Fields
    Themer
    Plugin Author

    Posted 2 years ago #

    What function are you using to define the categories variable? If you are using on of the supported filters from the Taxonomy Images plugin, you can use $cat->image_id.

    Maybe:

    $categories = apply_filters( 'taxonomy-images-get-terms', '' );

    I notice that you are using <?php print apply_filters( 'taxonomy-images-queried-term-image', '' ); ?>. This should not be in loops ... it was designed to be used only in term archive templates like category.php and tags.php.

  3. brettsmason
    Member
    Posted 2 years ago #

    Hi Michael

    Thanks for getting back to me. I actually pasted the wrong code, sorry! What I posted wasn't outputting the category details like I thought, it was: `<?php
    $cat = get_query_var('cat');

    $categories=get_categories('depth=1&child_of='.$cat);
    foreach($categories as $category) {
    echo $category->name;
    echo $category->description;
    }
    ?>`

    Sorry for my stupidity, but how would I incorporate your code into this? I have tried a few things without success.

  4. Michael Fields
    Themer
    Plugin Author

    Posted 2 years ago #

    Maybe something like this might work (untested):

    $categories = apply_filters( 'taxonomy-images-get-terms', '', array(
    	'term_args' => array(
    		'child_of' => $cat,
    		'depth'    => 1
    	)
    ) );
  5. brettsmason
    Member
    Posted 2 years ago #

    Thanks Michael

    With a bit of fiddling I have managed to get images outputting. However I don't think I'm doing it right as it's listing all of the categories. The code I have is:

    <?php
    $cat = get_query_var('cat');
    $categories = apply_filters( 'taxonomy-images-get-terms', '', array(
    	'term_args' => array(
    		'child_of' => $cat,
    		'depth'    => 1
    	)
    ) );
    foreach($categories as $category) {
    echo $category->name;
    echo $category->description;
    echo wp_get_attachment_image( $category->image_id, 'detail' );
    }
    ?>

    Still learning php so sorry if there's something glaringly obvious!

  6. brettsmason
    Member
    Posted 2 years ago #

    I'm still having no luck with this, I cant work out what I'm doing wrong. Any suggestions welcome!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic