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 following code to display images based on the category id:

<?php 
if (is_category( 'web-design' )){
?>   
<img src="<?php bloginfo('template_directory'); ?>/images/slider_webdesign.png" title="خدمات تصميم وتطوير المواقع" height="200px" width="960px" />
<?php
}else if (is_category( 'printing' )){
?>
<img src="<?php bloginfo('template_directory'); ?>/images/slider_printing.png" title="تصميم مطبوعات" height="200px" width="960px" />
<?php
}else if (is_category( 'online-marketing' )){
?>

I would like to make an array of only one condition to display an image with the category slug, is that possible?

share|improve this question
    
FYI, bloginfo('template_directory') is deprecated. Recommended function now is get_template_directory_uri(). –  mrwweb May 16 '12 at 20:59

1 Answer 1

up vote 1 down vote accepted

is_category() takes an array as arguments if you want to check for more than one category. But you can make it even easier with a check for existing files:

if ( is_category() )
{
    $slug       = get_queried_object()->slug;
    $theme_path = "/images/slider_$slug.png";
    $file       = get_template_directory() . $theme_path;

    if ( file_exists( $file ) )
    {
        echo '<img src="' 
            . get_template_directory_uri() . $theme_path 
            . '" alt="" height="200px" width="960px" />';
    }
}
share|improve this answer

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.