WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] If File Exists Post Image, if Not, post title. (8 posts)

  1. SueForPeace
    Member
    Posted 1 year ago #

    I'm having one hell of a time working this code out. Basically for pages, instead of posting the_title(), I want it to look for an image with the the_title() name and if it is available in the theme's dir/images/the_title().png to post the image, and if not, just post the title. Here is what I have:

    <?php
    $image = bloginfo('template_url') . the_title("/images/",".png");
    
    if (file_exists($image)) {
    		echo "you're in good shape";
    } else {
    		echo "yolo fool" . $image . "after stuff";
    }
    ?>

    first issue is that it echo's
    http://www.mywebsite.com/wp-content/themes/v1/images/Sponsors.png

    Which is the exact address of the image, and then it returns:
    yolo foolafter stuff

    So as you can see, I cannot use this variable to call the image. Any ideas?

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    It's not a good idea to use the post title as it can contain characters that are not allowed in filenames and can change at any time.

    Why not use custom fields to store the url or the filename of the image for the post?

    Or a featured image?

    If you still want to use the title and file_exists():
    Use get_the_title() if you want to store the title in a variable.
    I think file_exists() needs the path (not the url) to the image.
    To get the path to your template directory use get_template_directory and if you are using a child theme use get_stylesheet_directory
    And ask in a php forum how you can get it working with file_exists().

  3. SueForPeace
    Member
    Posted 1 year ago #

    Genius, thanks man, with your direction I came up with this:

    <?php
    $image = get_template_directory() . "/images/" . get_the_title() . ".png";
    $image2 = get_template_directory_uri() . "/images/" . get_the_title() . ".png";
    
    if (file_exists($image)) {
    		echo "<img src=" . $image2 . ">";
    } else {
    		echo the_title("<h1 class='title'>","</h1>");
    }
    ?>

    Works just fine, is there anything you see that can be condensed?

  4. SueForPeace
    Member
    Posted 1 year ago #

    better yet, like you stated, on pages, is there a if(featured_image!=null) { echo "<img>";
    } else {
    echo the_title();?
    }

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    You can check for a featured image with:
    http://codex.wordpress.org/Function_Reference/has_post_thumbnail

    Not sure if that's what you mean though.

  6. SueForPeace
    Member
    Posted 1 year ago #

    Thanks for the help, I've sorted all out with your direction, the perfect set up for me was

    <?php
    if (has_post_thumbnail()) {
    	the_post_thumbnail('full');
    } else {
    		echo the_title("<h1 class='title'>","</h1>");
    }
    ?>
  7. SueForPeace
    Member
    Posted 1 year ago #

    Thanks!

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you've got it resolved :-)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags