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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.