Simple PHP script for wordpress

Discussion in 'Programming' started by poizster, Sep 7, 2009.

  1. #1
    I was wondering if someone could help me out on a simple custom PHP code I need for my wordpress blog.
    All my posts on my blog have a different image in the top left that goes to the same external link.

    Currently if I want to change that link I have to update all 200 posts on my blog individually.

    I was looking for a snippet of code that I could put into the singlepost.php file (or any other way) that would allow me to put this external link in one place and all my posts would be updated.

    Thanks!
     
    poizster, Sep 7, 2009 IP
  2. tguillea

    tguillea Active Member

    Messages:
    229
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #2
    Well I think your best option is to update the code on all 200 so that it pulls the information on-the-go from a single file via SSI or PHP's include/require functions.

    for PHP:
    
    <?php require('image_and_link.php'); ?>
    
    PHP:
    for SSI (if for some reason PHP won't work):
    
    <!--#include file="image_and_link.php" -->
    
    Code (markup):
    and as for the file image_and_link.php, I'm not too familiar with wordpress but with a little creative renaming of image files and loop creation, the following will work. You will need to change this because I have no idea what a wordpress URL looks like, but if it were like a standard URL separated by "/", then this will work if you name your images after the respective page names (including the extension).
    
    <?php
    $currentfile = $_SERVER["SCRIPT_NAME"];
    $parts = explode('/', $currentfile);
    $page = $parts[count($parts) - 1];
    $images = scandir("directory/to/only/the/images/for/these/posts");
    echo '<a href="http://standard_constant_external_link.com"><img src="directory/to/only/the/images/for/these/posts/'.$images[$page].'.gif" border="0" /></a>';
    
    PHP:
    The trick is being able to call the image for that post specifically without giving it any information. In this case, I named the images after the web page in the URL, something that you can obtain (and is unique) on any PHP page.

    Let me know if this works out for you. As I said before, I know NOTHING about wordpress, only PHP :)
     
    tguillea, Sep 7, 2009 IP