• First off, I have searched extensively for a resolution or suggestions regarding this topic. I may not have titled the topic correctly.

    I am using this install of WordPress as a CMS and not a blogging tool. I chose to use WordPress because I did not want to deal with the bloated code that comes along with Joomla and Drupal. I also really do not need anything most of what is included in the core of those two platforms. As many already know, WordPress is much easier to template.

    Here is what I am trying to accomplish:

    I need these pages to dynamically generate a logo. There are 120 logos. Here is what I am thinking.

    Create a function that searches the page title, and if that title is found generate logo x.

    In addition, I have created 12 functions that will display depending on what page template is chosen for each page (located in user-functions.php which is called on in the themes functions.php file). I am wondering if there is a plugin, or a simple block of code that will allow me to edit the contents of this function in the admin area of wordpress

    I am somewhat new to WordPress, and still trying to fully understand the loop and how to plugin to the loop to accomplish what I need. Any advice on how to accomplish this would be greatly appreciated.

Viewing 15 replies - 1 through 15 (of 20 total)
  • So if some page with the title ‘hello’ is shown, you want to check if…

    some/path/ot/image/hello.jpg

    exists, and if it does, then load/include that image…

    Is that right?

    Assuming it is you’d need to check for the existence of spaces etc… I don’t think image src supports spaces so you’d need to think about how to handle that..

    If you can clarify what you want…

    Thread Starter taylor11

    (@taylor11)

    You hit it right on the head.

    By

    you’d need to check for the existence of spaces etc.

    do you mean spaces in the image name like…

    some/path/to/image/hello this is my image.jpg?

    Yeah i’m not sure if it’ll be a problem or not, i’m just considering a case when a title does have spaces, how would you want to handle that?

    Also are you referring to Pages or Posts, as often people refer to pages when they actually mean posts, and it’s easy to make that mistake…

    It should be a matter of …

    <?php
    $imgtype = '.jpg';
    $directory = '/some/path/to/files/';
    $somevar = the_title_attribute();
    
    if(strlen($somevar) < 5) {
      $logo = 'default';
    }
    elseif(file_exists($directory.$somevar.$imgtype)) {
      $logo = $somevar;
    }
    ?>
    <img src="<?php print $directory.$logo.$imgtype; ?>" alt="<?php print $logo;?>">

    Of course i’d need to test it, but that’s just an example…

    And that doesn’t take spaces into consideration… i just wanted to give an example…

    Thread Starter taylor11

    (@taylor11)

    These are pages, not post.

    Here is the code I am currently using, and it is working quite well. (Ignore that I don’t have “alt” tag)

    <?php
    if(is_page('Page Title'))
    { ?>
    <img src="/path/to/image.png" />
    <?php
    }
    elseif (is_page('Page Title'))
    {
    ?>
    <img src="/path/to/image.png" />
    <?php
    }
    ?>

    What I don’t like about doing this is that it is calling from the template file, making it rather messy. I will have to have 120 of these Conditionals to make it do exactly what I need.

    Well i make you something to work like the example i gave, but you still need to let me know how you want to handle titles with spaces since i don’t think you’ll be able to use them in an image src.

    I could remove all the spaces from the result…

    So let’s say for example there was a page titled…

    This is a page

    I could strip the spaces so it links to an image with the name like this…

    Thisisapage.jpg (.gif, whatever)

    Just need some clarification on my points then i’ll write the code to suit. In theory the example i gave in my previous post should work…

    Thread Starter taylor11

    (@taylor11)

    Thank you.

    Removing the spaces from the page title would be great. I was going to name the images according to a logo descriptor, but naming them by the page title will be logical as well. What happens if I have two locaton names(page titles) that are the same and each logo(image) is slightly different?

    I greatly appreciate your effort!

    I seem to be going in circles, file_exists function doesn’t want to play nicely when permalinks are on…

    I’ve tried using the image in the root and for some reason it always says the file does not exist, yet the image clearly appears on the page…

    Basically if i ask “does file exist”, it’s saying “no”, but the image does exist, which means the function is not working as it should.

    UPDATE WHILE WRITING REPLY::

    I knew i had file_exists in another theme, so went and tested that, it stil worked…

    So i’m just running a few more tests…

    Cuppa and ciggy at the ready, gonna figure this out… ??

    Ok, file_exists checks relatively, but relative links seem to end with the permalink structure attached to them when building the image src…

    Unfortunately file_exists relies on relative links and relative links on images don’t work correctly when permalinks are in place, so normally you’d rely on WP functions like bloginfo(”);, which doesn’t work with file_exists… (ironic)…

    Basically they don’t play nicely together (or not for me anywho), so i just needed to create 2 urls, one for the file_exists check and another to build the image URL…

    It’s now working…

    $imgtype = '.jpg'; // Image type, be sure to remember the period
    $directory = 'wp-content/test/'; // Where your files are located
    $ptitle = the_title('','',false); // The title, return=false so not to echo/return value
    $match_spaces = preg_match('/ /',$ptitle); // Check if there are spaces in the title
      if($match_spaces) : // If spaces found in title
        $logo = explode(' ',$ptitle); // Remove spaces
        $logo = implode('',$logo); // Join together again with delimter, none in this case
      else : // If no spaces, then explode/implode not needed
        $logo = $ptitle;
      endif;
    
    $logoresult = get_bloginfo('siteurl').'/'.$directory.$logo.$imgtype; // Build a complete path for image src
    $logodefault = get_bloginfo('siteurl').'/'.$directory.'default'.$imgtype; // Default image
    $logocheck = $directory.$logo.$imgtype; // Pure relative path for file_exists function
    
    $file = file_exists($logocheck);
    ?>
    <?php if($file) : // If the file exists ... ?>
      <img src="<?php print $logoresult; ?>" alt="<?php print $ptitle; ?>" />
    <?php else : // If the file does not exist, use default ... ?>
      <img src="<?php print $logodefault; ?>" alt="<?php print $ptitle; ?>" />
    <?php endif; ?>

    I placed 2 images into a folder called test (placed inside wp-content), 1 called “default” and another called “thisisapage”, both with .jpg extension. I then created a page called “this is a page”, then placed the above code into my theme’s existing page.php….

    Image shown on page:
    <img src="https://somesite/wp-content/test/thisisapage.jpg" alt="This is a page" />

    If i delete the file “thisisapage.jpg” the image becomes:
    <img src="https://somesite/wp-content/test/default.jpg" alt="This is a page" />

    Hopefully that works ok for you… ?? …

    Thread Starter taylor11

    (@taylor11)

    Wow! Worked flawlessly.

    This script combined with a gallery function will be perfect!

    I appreciate all your hard work.

    You’re welcome… it was quite enjoyable fiddling to make it work…

    Each problem solved is something new learnt… ??

    Thread Starter taylor11

    (@taylor11)

    One last thing. I am having trouble with.

    I’m attempting to use your same technique with preg_match to remove any apostrophes as well.

    I am having trouble getting it to output. Can this be easily accomplished?

    Thread Starter taylor11

    (@taylor11)

    Anyone have any ideas for this one based on t31os’s code?

    Ultimately would like to remove all characters except digits and alphabetical characters

    Backup the code you have, and try this instead, though it’s untested…

    <?php
    $imgtype = '.jpg'; // Image type, be sure to remember the period
    $directory = 'wp-content/test/'; // Where your files are located
    $ptitle = the_title('','',false); // The title, return=false so not to echo/return value
    $logo = ereg_replace('[^A-Za-z0-9]', '', $ptitle );
    $logoresult = get_bloginfo('siteurl').'/'.$directory.$logo.$imgtype; // Build a complete path for image src
    $logodefault = get_bloginfo('siteurl').'/'.$directory.'default'.$imgtype; // Default image
    $logocheck = $directory.$logo.$imgtype; // Pure relative path for file_exists function
    $file = file_exists($logocheck);
    ?>
    <?php if($file) : // If the file exists ... ?>
      <img src="<?php print $logoresult; ?>" alt="<?php print $ptitle; ?>" />
    <?php else : // If the file does not exist, use default ... ?>
      <img src="<?php print $logodefault; ?>" alt="<?php print $ptitle; ?>" />
    <?php endif; ?>

    It should work better the first method aswell, it just replaces anything not alphanumeric with nothing…

    So, for example…

    Hello my name’s bob!

    Would become….

    Hellomynamesbob

    Give it a shot…

    Thread Starter taylor11

    (@taylor11)

    Just tried it – I have been playing with this code all day. It is nearly exactly the same as you just provided. I was using str_replace

    $imgtype = '.png'; // Image type, be sure to remember the period
    $directory = 'wp-content/test/'; // Where your files are located
    $ptitle = the_title('','',false); // The title, return=false so not to echo/return value
    $logo = str_replace('[^a-zA-Z0-9]', '', $ptitle );
    
    $logoresult = get_bloginfo('siteurl').'/'.$directory.$logo.$imgtype; // Build a complete path for image src
    $logodefault = get_bloginfo('siteurl').'/'.$directory.'default'.$imgtype; // Default image
    $logocheck = $directory.$logo.$imgtype; // Pure relative path for file_exists function
    $file = file_exists($logocheck);
    ?>
    <?php if($file) : // If the file exists ... ?>
      <img src="<?php print $logoresult; ?>" alt="<?php print $ptitle; ?>" />
    <?php else : // If the file does not exist, use default ... ?>
      <img src="<?php print $logodefault; ?>" alt="<?php print $ptitle; ?>" />
    <?php endif; ?>

    I have no idea what is hanging it up! Been at it all day!

    I also have another issue you may be able to guide me with. I have 11 pages that are the same page name. Five of the pages with the same page name use a logo with a tagline, and six of the pages with the same page name use a logo without a tagline. To make a long story short, there are 11 pages with the same page name (Store name) and they share two different logos. Any ideas? I know I can hard code it and set it to use a special template, but it would be awesome if I could just have one template that handled it all!

    I’ll test the code again if you can’t get anywhere with it.

    What is output on the page if you add…
    echo $logo; or print $logo;

    Just below this line is fine..
    $logo = str_replace('[^a-zA-Z0-9]', '', $ptitle );

    Perhaps it’s expecting a delimeter, you could also try…
    $logo = str_replace('\[^a-zA-Z0-9]\', '', $ptitle );

    I’m pretty sure you shouldn’t need that though… but i don’t mind being wrong…

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Function to search page title and generate a logo’ is closed to new replies.