Support » Fixing WordPress » Putting a variable in a URL

  • Scenario:
    A file is needing to be referenced in several places.
    The name of this file changes often.
    The folder where this file is remains the same, so I just want to alter the filename.

    So the link would normally be:
    <a href="http://very/long/path/filename.txt">

    This makes for messy editing and increased chance of mistakes. So…. I want to include a file with the name in and then include that in the url as a constant:

    <?php include(‘constants.php’); ?>
    and that has
    $file1=rabbits.txt
    $file2=carrots.txt

    Then in the main code body
    <a href="http://very/long/path/<?php $file1 ?>">

    Unsuprisingly (because I’m writing it) this does not work and when I view source I see
    http://very/long/path/

    How do I do this ?

    Thanks 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • Just to make sure.
    <a href="http://very/long/path/<?=$file1?>">
    You print out that $file1 somehow, right?

    That’s what I want to do, yes.
    I need the $variable to become part of the url so it gets the right file.

    Try:

    <a href="http://very/long/path/<?php echo $file1; ?>">

    You just forgot the echo.


    <?php
    $cast1=file.txt;
    ?>

    <a href="http://very/long/url/<?php echo $cast1; ?>">

    That’s not working – output is as above, so I assume I’m doing something wrong. To be certain the variable is there, it is now in the header, not an include.

    Try

    <?php
    $cast1="file.txt";
    ?>

    You just forgot the quotes 😉

    McShelby – I had indeed 🙂

    The above does work perfectly, thank you !

    I then tried to embed the variable inside an Object tag – and failed. But that’s a different story…

    Okay, storytellers night. Tell me the whole story…

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Putting a variable in a URL’ is closed to new replies.