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.

On My category.php page i've got a list of categories with the title and the description.

Here's the code:

<?php 
$cat_id = get_query_var('cat');
$catlist = get_categories('hide_empty=0&child_of=' . $cat_id);
echo "<ul>";

foreach($catlist as $categories_item)
{
echo '<h1><a href="' . get_category_link( $categories_item->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $categories_item->name ) . '" ' . '>' . $categories_item->name.'</a> </h1> ';
    echo '<p>'. $categories_item->description . '</p>';
}
echo "</ul>";
}
?>

This code outputs categories that are children of the current category. For example if i'm on a category page called 'Products' the child categories of that will be shown.

I'm also using a plugin called Taxonomy Image, so i can assign an image to a category.

Here's the code that shows the image:

$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {

    foreach( (array) $terms as $term ) {
        print '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' );
    }

}

The problem is i want the markup like this.

<h1> category title
    <p> category description
       <img> category image

I've got the h1 and the p but if i want the image in the category list the code that displays my image needs to go inside the code that displays my categories.

This is where the problem lies, because all of the category images then get shown under every category. So under the first category title and description all of the images will be shown, then the same under the second category and so on.

How can i combine the code to get the markup i want and only show the correct image for each category and not all of the images?

I appreciate this might not be very clear but i've tried to explain it the best i can.

Thanks

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Something like this might work:

<?php 
$cat_id = get_query_var('cat');
$catlist = get_categories('hide_empty=0&child_of=' . $cat_id);
echo "<ul>";

foreach($catlist as $categories_item)
{
echo '<h1><a href="' . get_category_link( $categories_item->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $categories_item->name ) . '" ' . '>' . $categories_item->name.'</a> </h1> ';
    echo '<p>'. $categories_item->description;

    $terms = apply_filters( 'taxonomy-images-get-terms', '' );
    if ( ! empty( $terms ) ) {

      foreach( (array) $terms as $term ) {
        if($term->term_id == $categories_item->term_id) {
           print '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' );

        }
    }

}
    echo '</p>';
}
echo "</ul>";
}
?>

The point of the code is to only display the image if the term id of the image matches that of the category. Not the most efficient solution, but I am not clued up on the specific plugin you are using and what options you have for getting the image of a specific term.

share|improve this answer
    
Thanks. That seems to work the way i want it to. I will accept the answer once i've had a better look at it. –  Corbula Nov 28 '11 at 18:18
add comment

Why don't you just call the <ul> and </ul> outside the foreach. Hopefully I got this right. Not an expert.

<?php 
$cat_id = get_query_var('cat');
$catlist = get_categories('hide_empty=0&child_of=' . $cat_id); 
?>

<?php echo "<ul>"; ?>

<?php
foreach($catlist as $categories_item)
{
echo '<h1><a href="' . get_category_link( $categories_item->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $categories_item->name ) . '" ' . '>' . $categories_item->name.'</a> </h1> ';
echo '<p>'. $categories_item->description . '</p>';
<?php } ?>
your image can go here
<?php echo "</ul>"; ?>
share|improve this answer
    
Thanks, but that won't work. If i do this it just lists all of the images below all of the categories. I want the images to be put inside the list of categories in the markup. –  Corbula Nov 28 '11 at 17:03
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.