I need to add a php code in a wordpress widget called PHP Code Widget which only asks for proper enclosing:
All PHP code must be enclosed in the standard php opening and closing tags (
?> ) for it to be recognized and executed.
The code must use the current page title slug (i will use PAGES not posts) to look if in the upload folder there is any .jpg image named after the current page title SLUG. If there is such image, then it shows it:
<img src="current-page-name-slug.jpg" alt="current page title" height="image-height" width="image-width">
As you can see it must also use current page title for the image alt. And if possible, to retrieve image height and width and put them in their html parts. However this last one is not a must as i can manually resize all images to the same size and leave those fixed.
So the more or less pseudo code would be something like:
php start IF file exists uploads/current-page-slug.jpg then <img src="current-page-name-slug.jpg" alt="current page title" height="image-height" width="image-width"> else do nothing php end
Ive read a bit about the wordpress loop, and im not sure if current page slug and current page title are reacheable from a sidebar widget.
Give me a hand please.
I have tried bklynM code. It is not working and when looking in the page sourcecode to see what is going this is what i find:
<aside id="execphp-3" class="widget widget_execphp"><h3 class="widget-title">Image:</h3> <div class="execphpwidget"><img src="//www.yourdomain.com/wp-content/uploads/$slug" title="<?php the_title_attribute(); ?>" alt="" ></div>
(ive edited domain name to keep it private) In my understanding the code is mostly not being evaluated. Base url is evaluated, but the:
are untouched. Do you have any idea if maybe the plugin is preventing it from working. Also i use w3c total cache. Ive also updated my question to let clear that the img folder is fixed and images are not attached to posts in any way, only bulk uploaded through ftp to that folder. This is a way to programatically look if there is an image with the same permalink name (also called slug) that the page (again i dont use posts, i only use pages).What may am i missing here.
Update for bklinM:
Thank you very much this is working. However it does not excludes when there is no file so it shows an error on those situations. Ive read through link ive tried to add an else, to change it to a if_file_exists, to an is_file, and many other but they only break the code. Do you have any idea how to change the code so it does work only when it finds an image or an existing url to that file, so when there is no file no img html is shown. – Zacocom Zaccom