WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Linking to a page without hardcoding the URL (13 posts)

  1. Uh Oh Spaghetti Os
    Member
    Posted 1 year ago #

    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)

  2. Andrew
    Forum Moderator
    Posted 1 year ago #

    It's correct syntax but that function returns either an object or array http://codex.wordpress.org/Function_Reference/get_page_by_title

    What returned value are you looking for?

  3. Uh Oh Spaghetti Os
    Member
    Posted 1 year ago #

    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?

  4. Andrew
    Forum Moderator
    Posted 1 year ago #

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

  5. Uh Oh Spaghetti Os
    Member
    Posted 1 year ago #

    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

  6. Andrew
    Forum Moderator
    Posted 1 year ago #

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

    E.g:

    <?php var_dump( get_page_by_title( 'Register' ) );exit; ?>
  7. Uh Oh Spaghetti Os
    Member
    Posted 1 year ago #

    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)

  8. Andrew
    Forum Moderator
    Posted 1 year ago #

    Why don't you want to hardcode the URL?

  9. Uh Oh Spaghetti Os
    Member
    Posted 1 year ago #

    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

  10. Andrew
    Forum Moderator
    Posted 1 year ago #

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

  11. Uh Oh Spaghetti Os
    Member
    Posted 1 year ago #

    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!

  12. jabbo126
    Member
    Posted 1 year ago #

    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.

  13. Uh Oh Spaghetti Os
    Member
    Posted 1 year ago #

    Surely, then, if bloginfo('url') generates your homepage url, the code you supplied would direct you to http://www.site.com/home/desiredpage resulting in a 404?

    EDIT:
    Just tried it out, guess that isn't how it works

    Thanks again for the help, guys :)

Topic Closed

This topic has been closed to new replies.

About this Topic