Support » Fixing WordPress » Linking to a page without hardcoding the URL

  • Resolved Uh Oh Spaghetti Os


    Hi all,

    I’ve been trying to get this to work but can’t seem to

    I’m trying to link to a page within my site without having to link the entire URL as I will be moving the site to a different domain in a few weeks and don’t want to have to go through every link changing the URL

    Is it possible to link to a page without doing this?

    I hear that you’re supposed to use get_page_by_title(), but that function doesn’t seem to do anything

    <a href="<?php get_page_by_title('Register');?>"

    Is this the correct syntax? (Title of the page is Register)

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator Andrew Nevins


    Forum moderator

    It’s correct syntax but that function returns either an object or array

    What returned value are you looking for?

    Ah, right.
    What I want to do is to return a link to the page so that I can avoid having to enter the full URL for the same thing

    Is that possible at all?

    Moderator Andrew Nevins


    Forum moderator

    Try to var_dump that function and see whether you could salvage anything from it.

    Thanks for the replies

    Could you shed some light on what you mean by “var_dump that function”?
    I’m kind of new to WordPress and all that stuff so I’m not entirely sure what that is

    Moderator Andrew Nevins


    Forum moderator

    To see what value your function is returning, or what value a variable holds, you can use PHP’s built-in var_dump function.


    <?php var_dump( get_page_by_title( 'Register' ) );exit; ?>

    Thanks for the guidance

    Does any of this make sense to you?
    I can’t make head nor tail of it

    object(WP_Post)#17 (24) { ["ID"]=> int(32) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2013-05-02 10:40:29" ["post_date_gmt"]=> string(19) "2013-05-02 10:40:29" ["post_content"]=> string(2104)

    (this is what was returned from var_dump)

    Moderator Andrew Nevins


    Forum moderator

    Why don’t you want to hardcode the URL?

    I originally (ironically) wanted to avoid hardcoding the URL to avoid spending unnecessary time changing all my links when I swap my site to a different domain

    I suppose now it all seems moot, I’ve probably spent an equal amount of time trying to avoid wasting time! Lol

    Moderator Andrew Nevins


    Forum moderator

    I think in that case you should use the bloginfo function to generate the main body of the URL for you.

    Don’t you use that to access things from the theme folder though?
    The page templates are there, but not the pages. Is that correct to say?

    Although reading that page, it looks like the site_url() function might be what I’m looking for.. I’ll see if I can get this to work

    Thanks for your help!

    I’m sorry if i misunderstand your problem, but why don’t you simply use:

    <a href="<?php bloginfo('url');?>/your_page.php"> link </a>

    php bloginfo(‘url’) generates your homepage url relative to your domain.

    Surely, then, if bloginfo(‘url’) generates your homepage url, the code you supplied would direct you to resulting in a 404?

    Just tried it out, guess that isn’t how it works

    Thanks again for the help, guys 🙂

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Linking to a page without hardcoding the URL’ is closed to new replies.