Jump to content

WordPress PHP Q: getting a img filename and doing if/else


IcedEarth213

Recommended Posts

What I'm trying to do: For my WP site I'm building I have some custom image headings, so instead of <h1> tags, it would be an image.

 

I was trying to build a wordpress function where it would look and see if the page's title has a corresponding image (PNG), and if so, display it. If not, display the regular <h1> tag.

 

It would probably be best to do page slug instead of title though...not sure..here's what I have:

 

function title_image_check($title) {
    $image = '/images/' . $title . '.png';
    $filename = get_bloginfo('template_directory') . $image;

    if(file_exists($filename)) {
        $return =  '<span class="img-title"><img src="'.get_bloginfo('template_directory').$image.'" alt="'.$title.'" /></span>' ;
    } else {
        $return = '<h1 class="title">' . $title . '</h1>';
    }

    return $return;
}
add_filter('the_title', 'title_image_check');

 

It's currently broken, it always returns the 'else' value, even though I have an image named appropriately. Ideas?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.