WordPress.org

Support

Support » Themes and Templates » [Resolved] How to create links to Pages in theme

[Resolved] How to create links to Pages in theme

  • 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?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator Samuel Wood (Otto)

    @otto42

    WordPress.org Tech Dude

    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.

    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?

    Slug will work only if you use the nice permalinks.

    Moderator Samuel Wood (Otto)

    @otto42

    WordPress.org Tech Dude

    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.

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

    Thanks Moshu and Otto42! Your help is appreciated.

    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

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

    Bernhard

    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?

    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.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] How to create links to Pages in theme’ is closed to new replies.
Skip to toolbar