I have the following to display my list of categories:

      $categories = get_categories( $args );
  foreach ( $categories as $category ) {
     echo '<li><img src=""/><a href="' . get_category_link( $category->term_id ) . '" rel="bookmark">' . $category->name . '' . '' . $category->description . '</a></li>';
  }

But I am using the "categories images" plugin to assign an image to each category (which is what the

img src="" />

is for within the code. Within the src="" I need to get the following IF statement:

<?php if (function_exists('z_taxonomy_image_url')) echo z_taxonomy_image_url(); ?>

or even just the:

z_taxonomy_image_url();

How can I make this work please?

  • 1
    This question is pure PHP logic + third party plugin = off topic – cybmeta Nov 17 '14 at 17:38
  • Sorry cybmeta I disagree, the question was how to code an if statement inside an echo within Wordpress, I didn't know if it had to be in or out the loop etc and it's not specific to the plugin itself, the if statement could have been anything – Adrian Nov 17 '14 at 17:59
  • "if statement inside an echo" is pure PHP, WordPress makes absolutely no difference here. And your problem, acoording with the accepted answer, was only related with third party software. For me is celarly off-topci question. – cybmeta Nov 18 '14 at 8:17
up vote 3 down vote accepted

According to the plugins documentation (http://zahlan.net/blog/2012/06/categories-images/), this seems pretty simple:

<ul>
<?php 
$categories = get_categories( $args );
foreach ( $categories as $category ) {
    $img_src = z_taxonomy_image_url($category->term_id);
    if ( $img_src ) {
        echo '<li><img src="' . $img_src . '" alt="" /><a href="' . get_category_link($category->term_id) . '" rel="bookmark">' . $category->name . ' - ' . $category->description . '</a></li>';
    } else {
        echo '<li><a href="' . get_category_link($category->term_id) . '" rel="bookmark">' . $category->name . ' - ' . $category->description . '</a></li>';
    }
}
?>
</ul>
  • Beat me to it. +1 – iambriansreed Nov 17 '14 at 16:59
  • Thanks BODA, think we read this at the same time, had just amended this line: $imgurl = z_taxonomy_image_url($category->term_id); – Adrian Nov 17 '14 at 16:59
  • Updated, forgot a closing bracket. – BODA82 Nov 17 '14 at 16:59

You could try this.

$categories = get_categories( $args );
foreach ( $categories as $category ) {
     if (function_exists('z_taxonomy_image_url')) { 
          $imgurl = z_taxonomy_image_url(); 
     }
     if ( $imgurl ) {
          echo '<li><img src="'. $imgurl . '"/><a href="' . get_category_link( $category->term_id ) . '" rel="bookmark">' . $category->name . '' . '' . $category->description . '</a></li>'; 
     } else {
          echo '<li><a href="' . get_category_link( $category->term_id ) . '" rel="bookmark">' . $category->name . '' . '' . $category->description . '</a></li>';
     }
}
  • Thanks for the reply, tried this but just returns empty as if there is no image set – Adrian Nov 17 '14 at 16:52
  • Have fixed it by changing this line: $imgurl = z_taxonomy_image_url($category->term_id); – Adrian Nov 17 '14 at 16:59

Here's my take on it:

$categories = get_categories( $args );
foreach ( $categories as $category ) 
{
    $image = (function_exists('z_taxonomy_image_url') && z_taxonomy_image_url($category->term_id) ) ? sprintf('<img src="%s">', z_taxonomy_image_url($category->term_id) ) : '';
    echo sprintf('<li>%s<a href="%s" rel="bookmark">%s %s</a></li>', $image, get_category_link( $category->term_id ), $category->name, $category->description);
}

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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