WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to create links to Pages in theme (10 posts)

  1. belminf
    Member
    Posted 6 years ago #

    I want to create a link in a template to certain pages. Now I could just write it out to what the current URL is now but I wanted to know what is the most flexible way to do this if I already know the slug? Is there a function for this?

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

    If you know the page's ID number:
    echo get_permalink(ID_NUMBER_HERE);

    Works for pages, posts, or attachment posts. Even works correctly with a static front page.

  3. belminf
    Member
    Posted 6 years ago #

    I would feel more comfortable using Slug as oppose ID.

    But thank you very much! Didn't know get_permalink(ID) worked for Pages as well. I started using this:


    <?php bloginfo('url')?>/slughere

    But is this really the best way? *Shrug* I guess I can't think of how something else would be better. Perhaps there's a built-in function I can't find?

  4. moshu
    Member
    Posted 6 years ago #

    Slug will work only if you use the nice permalinks.

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

    I would feel more comfortable using Slug as oppose ID.

    Why? The slug can be changed by the editor or admin and such. The ID can't. Referring to a post or page by the slug is not nearly as safe (long term) as using the ID number is.

    get_permalink() is the best way because it's always guaranteed to be the correct canonical URL.

  6. belminf
    Member
    Posted 6 years ago #

    Thanks Otto. I thought the slug would be safer but I guess I was wrong :-&.

    Thanks Moshu and Otto42! Your help is appreciated.

  7. drkite
    Member
    Posted 6 years ago #

    Hmm, and what, if I do not know the ID of the page?

    I would like to have a link simply to next page, e.g. if the page of September 19th is displayed, then on the top I want to have a link to whatever page was published afterwards (September 20th??, or October 2nd??)

    Any suggestions?

    Bernhard

  8. drkite
    Member
    Posted 6 years ago #

    Sorry, I hate to admit... stupid me... RTFM
    Here is the solution:
    Codex link

    Bernhard

  9. MACscr
    Member
    Posted 6 years ago #

    i dont know what im doing wrong, but when I do:

    echo get_permalink(27);

    It only returns the general site url, does not return any type of url to the page im linking to. Heck, it dosent even include the location to the actual wordpress install. It just gives domain.com, versus domain.com/blog/page/

    Any ideas?

  10. MACscr
    Member
    Posted 6 years ago #

    eh, i got it fixed. It was working fine, i guess my css was just hiding the rest of the url. Once i viewed source, i could see the entire url.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags