• Resolved magdalenagirl

    (@magdalenagirl)


    I’m trying to add a script that will randomly rotate a set of images inside a page post.

    I already tried this:

    <script type="text/javascript" src="/scripts/updatepage.js"></script>
    <script type="text/javascript">
    <!--
    updatepage();
    //--></script>

    and it didn’t work.

Viewing 15 replies - 16 through 30 (of 33 total)
  • Yes, share the url.

    I’m also curious why you aren’t just writing this in PHP? If that is the sum of your javascript, I don’t see the need for the javascript.

    Thread Starter magdalenagirl

    (@magdalenagirl)

    how do i share the url?

    I’m going to try ashfame’s suggestion…although I’m not sure I understand the php code completely and where to put it. the site doesn’t tell you.

    how do i share the url?

    You can’t. Sorry. You said you were using an offline server.

    The PHP equivalent would be extremely simple.

    $numberofimages = 10;
    echo '<img src="'.get_bloginfo('template_url').'/top'.rand(1,$numberofimages).'.gif" />';

    This will display a random image between top1.gif and top10.gif every tiem the page loads, just like your javascript does.

    Thread Starter magdalenagirl

    (@magdalenagirl)

    where do i put that code?

    Put the code wherever you want the images to show up. The only catch is that it needs to go inside PHP tags– <?php // the php image code ?>. With PHP you can drop in and out of html using those tags. Inside the tags code will execute as PHP. Outside the tags code will execute as ordinary html. So if you find a spot inside existing PHP tags you don’t need to add them, but if you want to insert the code in a spot that is outside existing php tags, you need to add the tags yourself. It really isn’t conceptually much different from wrapping a javascript in a <script> tag. Based on your previous code posting, you’d replace this:

    <script type="text/javascript" language="JavaScript"><!--
    printImage();
    //--></script>

    with this:

    <?php
    $numberofimages = 10;
    echo '<img src="'.get_bloginfo('template_url').'/top'.rand(1,$numberofimages).'.gif" />';
    ?>
    Thread Starter magdalenagirl

    (@magdalenagirl)

    this is what it shows me when i go to view the page:

    ‘; ?>

    It works when I paste it in. You’ve a minor syntax error somewhere. Paste image code and about five lines above and below it. I think you probably have mismatched opening/closing PHP tags.

    Thread Starter magdalenagirl

    (@magdalenagirl)

    above and below what?

    Above and below the part you added– the part that is giving the error.

    Thread Starter magdalenagirl

    (@magdalenagirl)

    nop, nothing.. didn’t work
    i appreciate your help.

    can i see where you’re testing it?

    What didn’t work? I asked you to post the code. You didn’t. I can’t sort out the syntax error if I can’t see your code.

    You can’t really see where I’m testing it because its on a local development server but here is the complete template file I’m using, if it helps.

    <?php
    /*
    Template Name: cat5
    */
    get_header(); ?>
    <div class="subpanel">hi</div>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php
    $numberofimages = 10;
    echo '<img src="'.get_bloginfo('template_url').'/top'.rand(1,$numberofimages).'.gif" />'; ?>
    <?php endwhile; else: ?>
    <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>
    Thread Starter magdalenagirl

    (@magdalenagirl)

    ohhh…sorry i didn’t understand what you were saying.

    you’re adding the the code directly into the page file.

    i’m adding it inside an actual post, on the wordpress admin, under add new page.
    i want it only on one page, not all pages…

    Okay. I see the problem. I didn’t realize you were talking about pasting this into the editor. PHP won’t execute if pasted into a post, a page, or a text widget (maybe other places as well), though you can use PHPExec to change that.

    i want it only on one page, not all pages…

    I understand. You can select which template file that WP uses on a page by page basis. For example:

    1) Take the code I gave you and create file named cat5.php in your template directory.
    2) Go to wp-admin->Pages->Add New and create a page selecting cat5 from the menu on the right under ‘Template’. Let’s say you give this page the title ‘My New Page’.

    Now, the page titled ‘My New Page’ is the only page that will use the cat5 template, although you could assign the template to other pages later if you want. See how that works? That is what I thought you were trying to do.

    Thread Starter magdalenagirl

    (@magdalenagirl)

    omg it worked (duh), thanks so much!!

    i installed phpexec and then added:

    <?php
    $numberofimages = 10;
    echo '<img src="'.get_bloginfo('template_url').'/top'.rand(1,$numberofimages).'.gif" />';
    ?>

    inside the post and it worked! thanks again!

    ^_^

    Thread Starter magdalenagirl

    (@magdalenagirl)

    and thanks for the info about the template option. i didn’t know about that. i might use that sometime.. thanks!

Viewing 15 replies - 16 through 30 (of 33 total)
  • The topic ‘adding javascript into an individual page’ is closed to new replies.