• I have this function in php:

    function catimage($wptitle){
    $wptitle = strtolower(str_replace(‘ ‘,”,$wptitle));
    $path = get_bloginfo(‘template_url’).’/images/’;
    $file = $path.$wptitle.’.png’;
    return $file;
    }

    and it is called from header.php as catimage(wp_title(‘ ‘,0)) so that i strip the category name of spaces and test if there is any image in the specified directory.

    the problem is that the index page is not a category and it doesn’t load any image even if i have one for it. How can i test, inside this function, if the string returned by the wp_title is zero of null or whatever the function returns if the page is the index page and assign a default image so it is displayed when no image is prepared for that category.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Maybe I’m misunderstanding you, but why not just

    if ( '' == $wptitle )

    ?

    Thread Starter da_silva

    (@da_silva)

    i tried in many ways to do it but i’m confused because it doesn’t work. I tried with
    if ($wptitle=''){$file=$path.'homecoffee.png'} and it didn’t work, now i tried your solution like this:


    function catimage($wptitle){
    $wptitle = strtolower(str_replace(' ','',$wptitle));
    $path = get_bloginfo('template_url').'/images/';
    $file = $path.$wptitle.'.png';
    if( '' == $wptitle ){$file=$path.'homecoffee.png';}
    return $file;
    }

    …and nothing. What could be the problem?

    if ($wptitle='') doesn’t work because you’re using only one =

    In other words, you’re setting $wptitle to ”

    I don’t know why your function doesn’t work; I was taking your word for it that wp_title() returns an empty string on the home page.

    If you want to test for the home page, why not just use is_home() ?

    Thread Starter da_silva

    (@da_silva)

    i don’t know, it seems to me that either the function wp_title() doesn’t return an empty
    string when there is nothing to show or my if statement is wrong, i don’t really understand the inner php of it and it loocs too complicated to me. I did another trick by counting the letters in the $file, which are at a minimum when the last characters are ‘/.png’ meaning no title and they are 66 in my case. So when the count is 66 then the file being browsed is index.php. But i’m not happy with the solution so i’d like to try the existance of the file with file_exist() but i don’t know how the path should be. I tried with if (file_exists($file)){echo exists;} but no success.
    I admitt i suck at php but can anyone here who knows the language what am i doing wrong?

    Perhaps you could explain more what you’re trying to accomplish. For example, are the images you’re linking to tied to individual posts? How do you have them organized?

    Thread Starter da_silva

    (@da_silva)

    ok, let me explain i hope you will understand: my theme is in ‘/themes/coffeecup/’. Also there is a directory named /’images’ inside the theme directory. In this dir are all the images that compose the layout.
    Now, i want to use as much as possible the index.php to get my pages displayed so i won’t use category templates, not even archives.php or pages.php. I want everything to be loaded as appropriate within the index.php. So besides my style.css i have a few css ids that have a background image which i try to load dinamicly through php. So i have this call from the style in the header.php:

    <style type="text/css">
    <!--
    div#chest{background-image:url(<?php echo catimage(wp_title(' ',0)); ?>);
    background-repeat: no-repeat;
    height: 185px;
    }
    -->
    </style>

    Here the function catimage() must return the string with a path to the file whose name is identical with the category or Page the visitor is browsing or another file with general meaning if the file doesn’t exist.

    I take the wp_title(' ',0) and trim all spaces, get it lowercase and try to find a match in the ‘/image’ directory in the theme. If there’s one display it if not display the general purpose image.

    I’ve foud earlier that file_exists() works only if the path is ‘wp-content/themes/coffeecup/’ so what the get_blog_info() and bloginfo() functions in’t going to work as long as they provide a string that looks like ‘http://localhost/wordpress/wp-content/themes/coffeecup&#8217; so i must trim it. But i would have liked to be able to search for that file in the whole directory , recursively, so if an image for some later added category isn’t in ‘/coffeecup/images/’ than it will still be found, but i’m researching that right now for a function to do that.

    Hope this explains :d. Perhaps sounds complicated but i try as much as possible that all work in the functions to be done only once, then all to be done from the admin view.

    ah, and to answer your question: no, the images are just for category illustration purposes.

    the images are just for category illustration purposes.

    If you want there to be a particular image for each category, get_query_var('cat') will return the requested category’s id number.

    The global variable $id will give you the page id, so you can set page-specific images.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘php simple function but novice here.Pls Help!’ is closed to new replies.