• Resolved kevin22

    (@kevin22)


    I’m currently trying to add a randamized tip section to my wordpress sidebar. I’ve tried using a widget but it disposes of all the other info I have there already when I activate the widget, I’m told that most times there is some ‘default content’ coded in that disappears as soon as you put a widget into the sidebar.

    So I tried a software solution. First I created a text file called tips.txt and added tips one per line e.g.
    tip one
    tip two
    tip three and so on

    Next I created a tips.php file using the following

    <?php
    
    $hash = floor(time() / (60*60*24));
    srand($hash);
    
    $tips = array_map("trim", file("tips.txt"));
    
    $tip = $tips[array_rand($tips)];
    echo $tip;
    
    ?>

    And finally I added this line to my sidebar: <?php require("tips.php"); ?> and them ftp them via Dreamweaver.

    All the files are in the same directory ie the brandford directory

    However, I get the following error messages:

    Warning: file(tips.txt) [function.file]: failed to open stream: No such file or directory in /websites/LinuxPackage04/ba/bi/es/babies-worldwide.com/public_html/wp-content/themes/branford with mimbo sidebar/tips.php on line 6

    Warning: array_map() [function.array-map]: Argument #2 should be an array in /websites/LinuxPackage04/ba/bi/es/babies-worldwide.com/public_html/wp-content/themes/branford with mimbo sidebar/tips.php on line 6

    Warning: array_rand() [function.array-rand]: First argument has to be an array in /websites/LinuxPackage04/ba/bi/es/babies-worldwide.com/public_html/wp-content/themes/branford with mimbo sidebar/tips.php on line 8

    Any idea what might be wrong, and more still how to fix it.

    Kevin

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter kevin22

    (@kevin22)

    So OK no takers on why the script doesn’t work; I’m still open to suggestions but maybe going down the widget road would be easier?

    As mentioned above put a rotator type widget, or any widget for that matter, on a wordpress blog where you already have other standard info like: catagories, ad blocks, archives, contributors, that general type of stuff activate the widget and hey presto all your info has gone.

    Can anyone tell me how you actually put a widget onto a wordpress blog without all the other info disappearing. I’m hearing that this is one of the drawbacks of wordpress widgets – I’m digging through past posts to see if I can find anything useful. Or maybe I’m just silly, any advice?

    Kevin

    For info on writing widgets this is a good starting point.
    https://www.lonewolfdesigns.co.uk/create-wordpress-widgets/

    The errors tell you what’s wrong regarding your code, take this bit for example.
    $tips = array_map("trim", file("tips.txt"));

    You can’t map the array because you’re not feeding it an array.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Adding a randomized script’ is closed to new replies.