1

I want to implement this <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /> into the following function.

function list_my_categories($exclude = '') {
    if (!empty($exclude)) {
        $exclude = 'exclude=' . $exclude;
    }
    $categories = get_categories($exclude);
    $html = '';

    foreach ($categories as $cat) {
            if($cat->category_parent == 0) {
            <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
                $html .= '<p>' . $cat->cat_name . '</p>';
                                $html .= '<p>' . $cat->category_description . '</p>';
                $childcategories =  get_categories('child_of='.$cat->cat_ID.'');
                        if(!empty($childcategories)){
                  foreach ($childcategories as $ccat) {
                  $html .= '<p>' . $ccat->cat_name . '</p>';
                              $html .= '<p>' . $ccat->category_description . '</p>';
                      }
                        }
               }
    }

    return $html;
}
add_shortcode('show-cats', 'list_my_categories');

The function shows a list of categories in WordPress, and I have another plugin which allows you to set an image for each category, so i am trying to show it...

1 Answer 1

2
if($cat->category_parent == 0) 
{
    $html .= '<img src="' . z_taxonomy_image_url($cat->term_id) . '" />';
    $html .= '<p>' . $cat->cat_name . '</p>';
    $html .= '<p>' . $cat->category_description . '</p>';
    $childcategories =  get_categories('child_of='.$cat->cat_ID.'');
    if(!empty($childcategories))
    {
        foreach ($childcategories as $ccat) 
        {
            /*$html .= '<img src="' . z_taxonomy_image_url($ccat->term_id) . '" />';*/
            $html .= '<p>' . $ccat->cat_name . '</p>';
            $html .= '<p>' . $ccat->category_description . '</p>';
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is the function i am using written well? or is there a better way of performing the same function but better coded?
it could be improved if you were to seperate the html from the php. it may save you a headache when you next need to change the output of the HTML or use the code somewhere else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.