• Hello, I am using WP 3.5.1, twenty twelve child theme and PHP 5.2 on my server.
    I am trying to use a php script(that works on my other websites) in order to get random background-image but it’s not working:

    CSS:

    body {
    	background: url(<?php echo $selectedBg; ?>) no-repeat fixed;
    }

    PHP:

    <?php
      $bg = array('1.jpg','2.jpg','3.jpg','4.jpg','5.jpg');
      $i = rand(0, count($bg)-1);
      $selectedBg = "$bg[$i]";
    ?>

    my php file is in the same folder as the jpg’s.
    Any ideas?

Viewing 4 replies - 1 through 4 (of 4 total)
  • I am using WP 3.5.1

    Why? The current version of WordPress is 3.5.2.

    my php file is in the same folder as the jpg’s.

    1. How is this script being called in your theme?
    2. You need to output absolute (full) image urls – not relative ones.

    Thread Starter zefs

    (@zefs)

    1. I use php include.
    2. I tried that(doesn’t work).

    It seems that whenever I try to parse PHP in CSS it just doesn’t work, any idea why? only happens on my wordpress site, not on my other static websites on the same server.

    I use php include.

    The standard PHP include() doesn’t always work predictably in a WordPress theme context. You might be better off using get_template_part() or turning your script into a function within your theme’s function.php file that you can then call when needed.

    I tried that(doesn’t work).

    Have you examined the markup/urls generated by the script? Or doesn’t it get that far?

    whenever I try to parse PHP in CSS it just doesn’t work

    WordPress doesn’t do anything different with CSS than the standard web page/site, so it’s probably an issue with your CSS specificity. Try using Firefox with the Firebug add-on for this kind of CSS troubleshooting. Or use whatever developer tool is available in your web browser.

    Thread Starter zefs

    (@zefs)

    The css(first post) works on my none-wordpress sites as I said.

    But for that wordpress site it doesn’t, on chrome inspector I get this error whenever I try to parse php in the background:url property of the theme’s style.css: “invalid property value”.

    I tried both full and relative paths with no luck.
    Could it have to do with me deleting the .htaccess file after the wordpress installation?

    I also asked on stackoverflow, none of the solutions worked:
    https://stackoverflow.com/questions/17291408/random-background-images-in-wordpress

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Random background image php’ is closed to new replies.