WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] find what page I'm on (8 posts)

  1. elDeuce
    Member
    Posted 6 years ago #

    Okay, so I know very little PHP, but I did some research and pieced together

    $domain = $_SERVER['HTTP_HOST'];
    $path = $_SERVER['SCRIPT_NAME'];
    $page_url = "http://" . $domain . $path;

    this allows me to echo $page_url and it tells me the URL of the page I am on. This works swimmingly on a static site, but when i use it on wordpress I always get http://www.doamin.com/index.php no matter what page I am on.

    Basically I need one header graphic for the Home page and I need a second header graphic for all interior pages. I am doing this with IDs on a H2 tag. I just need a way to tell the script what page I am on means which ID to put in the H2 tag, but if i can't get $page_url to pull out the right url then I'm not sure how to do this.
    I am sure it's an issue with mod_rewrite or something.

    Maybe there is another way of doing this?

    Thanks for the help

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    You're doing it the really hard way. Try this instead:

    if (is_home()) {
    echo 'This is the home page!';
    } else {
    echo 'This is NOT the home page!';
    }

    Simple, eh? These are called Conditional Tags in WordPress, and they're handy for this sort of thing.

  3. elDeuce
    Member
    Posted 6 years ago #

    You made this way too easy :-)

  4. elDeuce
    Member
    Posted 6 years ago #

    Maybe I spoke too soon.
    I have

    <?php
        if (is_home()) {
          echo '<h2 id="idxhdr">Welcome to the site</h2> ;
        } else {
          echo '<h2>' . the_title() . '</h2>';
        }
        ?>

    but when it parses, i get

    Page Title<h2></h2>

    Any idea what I did wrong?

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    This line:
    echo '<h2>' . the_title() . '</h2>';

    Is what's giving you the Page Title outside the h2 tags. Try this instead:

    echo '<h2>';
    the_title();
    echo '</h2>';
  6. elDeuce
    Member
    Posted 6 years ago #

    Worked like a champ.
    Thanks for your help. Those conditional statements have come in very handy for putting embedded video on certain pages (the code box wouldnt let me paste the code) and then i would able to php include forms on certain pages.

    Site is finally coming together... only took me 5 hours so far.

  7. elDeuce
    Member
    Posted 6 years ago #

    I keep talking too soon.
    Once I change the else statement to your way of writing it, it stops doing the correct header for the home page, now ever the home page has the secondary header.

    ....

    <?php
        if (is_home()) {
          echo '<h2 id="idxhdr">Home Page</h2> ';
        } else {
          echo '<h2>';
          the_title();
          echo '</h2>';
        }
        ?>
  8. elDeuce
    Member
    Posted 6 years ago #

    I just used

    is_page('home')

    and this works. I'll just stick with this.
    Thanks for the help.

Topic Closed

This topic has been closed to new replies.

About this Topic