Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the Taxonomy Images plugin to try to pull in the category images for custom categories on a page that displays all the categories my site has. For some reason, this code outputs the correct names and descriptions, but will only show one category image.

I'd like the images to do what the other category features do, populate with the appropriate data for each category and display all the categories on the page. If I use echo wp_get_attachment_image( $imgs[$id], 'thumbnail' ); it publishes all of the images, but if I change that from echo to make it a variable, it only displays one image.

$imgs = get_option( 'taxonomy_image_plugin' );
$categories = get_terms('portfolio_category', array('orderby' => 'id', 'hide_empty' => 0));
foreach ( $categories as $category ) {
    $id = $category->term_id;
    $img = wp_get_attachment_image( $imgs[$id], 'thumbnail' );
}
    for($i=0; $i<count($categories); $i++)
     {
     echo('<h2>'.$categories[$i]->name.'</h2>');
     echo($img);
     echo('<p>'.$categories[$i]->description.'</p>');
}

I guess that I'm just confused as how to add a counter like [$i] so that this code outputs the correct category images. Any help would be much appreciated.

share|improve this question
    
"...a page that displays the portfolio categories of a theme I've purchased..." - Close-voted as too localized. As this is a commercial Theme, we don't have access to the source code to be able to help diagnose/resolve the issue. Have you tried the support offerings of the developer from whom you purchased the Theme? –  Chip Bennett Dec 10 '12 at 19:55
    
I have been in contact with the support team, I've just found that Stack Exchange is faster and better with with this sort of stuff. I've provided all the code that should be necessary to debug my problem(hopefully), and the features that I'm using like get_terms() and Taxonomy Images are either open source plugins or built in to Wordpress, is there a way I could rephrase this question so that it can not be closed? –  elkirkmo Dec 10 '12 at 19:58
    
This line is the problem: $cats = pexeto_get_taxonomy_children('portfolio_category', $cat_id);. The pexeto_get_taxonomy_children() is a Theme function, not a core function. –  Chip Bennett Dec 10 '12 at 20:01
    
I know, but that's the part that works. I'm trying to get the category descriptions and images to work. It's fine, I understand the protocol here and hopefully the people at pexeto can help me figure out my problem. Thanks. –  elkirkmo Dec 10 '12 at 20:02
    
Then can you edit your question to specify the output of that function? If so, then the rest of the question should involve only core functions. Also, clarify which functions are returning what you expect, and which ones aren't. –  Chip Bennett Dec 10 '12 at 20:05
show 5 more comments

2 Answers 2

I think (at least part of) the problem is that you've got a nested loop where you don't really need one.

The output of get_terms() is an array of objects. So $categories should be an array of objects.

You loop through this array of objects here:

foreach ( $categories as $category ) {
    $id = $category->term_id;
    $img = wp_get_attachment_image( $imgs[$id], 'thumbnail' );
}

The problem is that you close that loop, and then loop through $categories a second time:

for($i=0; $i<count($categories); $i++)
     {
     echo('<h2>'.$categories[$i]->name.'</h2>');
     echo($img);
     echo('<p>'.$categories[$i]->description.'</p>');
}

The problem there, though, is that $id and $img will retain their value based on the last object in $categories.

I would recommend getting rid of the second loop, and doing everything in your original foreach loop:

foreach ( $categories as $category ) {
    // Set ID and IMG
    $id = $category->term_id;
    $img = wp_get_attachment_image( $imgs[$id], 'thumbnail' );

    // Output markup
     echo('<h2>'.$category->name.'</h2>');
     echo($img);
     echo('<p>'.$category->description.'</p>');
}

That should fix the issues with out-of-sync looping, though I'm not entirely certain about $img, since I'm not familiar with the Plugin you're using and don't know how it saves its option to the database.

share|improve this answer
    
I need to keep the second loop open, because I'm trying to display all the categories I've created plus more in the future. So the for($i=0; $i<count($categories); $i++) loop is necessary on that end. The problem I'm having is getting the image based on the $category->term_id; for every category. The page is here and hopefully seeing it will give you a general sense of what I'm trying to accomplish, just with different images on each category. –  elkirkmo Dec 11 '12 at 1:45
    
You're using this line to query all categories: $categories = get_terms('portfolio_category', array('orderby' => 'id', 'hide_empty' => 0)); Why would that fail to query new categories that may be added in the future? –  Chip Bennett Dec 11 '12 at 2:36
    
It's failing to add the rest of the categories that currently exist. This code only displays the last category. Although, all the information is correct, so that part is working finally! –  elkirkmo Dec 12 '12 at 19:04
    
I was able to figure it out thanks to your help. Kudos to you and your magnificent Wordpress savvy. –  elkirkmo Dec 12 '12 at 20:56
add comment
up vote 0 down vote accepted

Chip was on the right path, but closing the first loop would only publish one category instead of every category. What I ended up doing was closing the first loop and calling the ID and the img inside the second loop.

$imgs = get_option( 'taxonomy_image_plugin' );
$categories = get_terms('portfolio_category', array('orderby' => 'id', 'hide_empty' => 0));
for($i=0; $i<count($categories); $i++)
    {
    $id=$categories[$i]->term_id;
    $img=wp_get_attachment_image($imgs[$id], 'thumbnail');
        echo('<h2>'.$categories[$i]->name.'</h2>');
        echo($img);
        echo('<p>'.$categories[$i]->description.'</p>');
    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.