1

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?

3
  • 1
    This question is pure PHP logic + third party plugin = off topic
    – cybmeta
    Nov 17, 2014 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, 2014 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, 2014 at 8:17

3 Answers 3

3

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>
3
  • Beat me to it. +1 Nov 17, 2014 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, 2014 at 16:59
  • Updated, forgot a closing bracket.
    – BODA82
    Nov 17, 2014 at 16:59
0

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>';
     }
}
2
  • Thanks for the reply, tried this but just returns empty as if there is no image set
    – Adrian
    Nov 17, 2014 at 16:52
  • Have fixed it by changing this line: $imgurl = z_taxonomy_image_url($category->term_id);
    – Adrian
    Nov 17, 2014 at 16:59
0

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

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

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