WordPress.org

Ready to get started?Download WordPress

Forums

How to insert a link to a Page (7 posts)

  1. thanatica2
    Member
    Posted 4 years ago #

    I want to insert a link to a static page, say, my contact page. I know I can put a static link like "index.php?page_id=42" but that won't do. I'm using pretty url's and I want WP to generate the pretty url for me.

    Of course, I could put a link like "/contact", but that won't do either, since I want the link to work on a dev environment (at http://www.example.com/yadayada/wordpress) and the live environment (at http://www.yadayada.com). Plus the link must keep working even after changing the permalink, structural location, or title of that page. Hence, referring to the page ID is a good idea, but I don't want it in the HTML.

    So to my knowledge, there's nothing I can put in a plain vanilla <a> tag that covers this. Basically I'd like a code or something that generates a link just like wp_list_pages() does.

    How do I do that? Plugin? Please help :)

    I'm hoping for something like <a href="$page_id=42$">Contact</a>, but I'm pretty sure that won't cut it ;)

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    If you have a page called "Contact" in a site where WP is installed in a folder called "wp" and you have a custom permalink structure, the page link will always be http://domain.com/wp/contact/. Page permalinks don't change if you change your custom permalink to another custom structure.

  3. thanatica2
    Member
    Posted 4 years ago #

    Oh? So let's be clear on this: I create a contact page at the root level. It's permalink is then set at "/contact".

    Now I change the title to "Getting in touch". How will WP know that "/contact" is really meant to be "/getting-in-touch"?

    Another situation: I move the contact page to being a sub-page of "company info", whereas the permalink would become "/company-info/contact". Would "/contact" still work? And what if I happen to create yet another contact page?

    Also, I still don't have an answer to how to write the link in the HTML. I don't know in the content what the base url is (live versus developement, for example) and I can't use relative paths due to the nature of "pretty" urls. So I still don't know how to go about this.

  4. buddha trance
    Member
    Posted 4 years ago #

    Now I change the title to "Getting in touch". How will WP know that "/contact" is really meant to be "/getting-in-touch"

    You can edit the permalink slug (it's just under the title field, on page edit mode)

    Another situation: I move the contact page to being a sub-page of "company info", whereas the permalink would become "/company-info/contact".

    Under attributes, you make the page a "child of" another page, and the permalink changes automatically.

    And what if I happen to create yet another contact page?

    Fine, since the previous one is now a sub page.

  5. buddha trance
    Member
    Posted 4 years ago #

    Also, I still don't have an answer to how to write the link in the HTML.

    <a href="<?php bloginfo('url'); ?>/your-page.php">Your Page</a>

  6. thanatica2
    Member
    Posted 4 years ago #

    You can edit the permalink slug (it's just under the title field, on page edit mode)

    But then I would have to force the url to mismatch the title. Not good for accessibility.

    Under attributes, you make the page a "child of" another page, and the permalink changes automatically.

    And I would have to manually update all pages that refer to that permalink. That's part of the problem I'm trying to solve.

    Fine, since the previous one is now a sub page.

    Not fine, since the link now refers to the wrong page.

    Also PHP code in the content is ugly, not user-friendly, and error-prone. IF it even works to begin with.

    But not to worry. I have created a simple plugin that does what I need.

    <a href="$42$">Contact</a>
    becomes
    <a href="/yadayada/wordpress/contact">Contact</a>
    Or whatever the permalink would be.

    This way I don't have to hassle with static links in my content. The ID never changes, so the links will always be correct, no matter what the permalink is.

    I still think it's odd that wordpress doesn't provide a mechanism similar to this. The "link" button in the editor only allows to enter a fully qualified url, which is completely useless for linking to pages, categories, tags, post, etc.

    (btw, don't you think wordpress defies the purpose of permalinks a little? "perma" stands for "permanent", but on wordpress, permalinks are by no means permament...)

  7. esmi
    Forum Moderator
    Posted 4 years ago #

    But then I would have to force the url to mismatch the title. Not good for accessibility.

    The match between the url and the page title doesn't impact on accessibility (although it may have a minor SEO impact). All of the AT agents that I know of will render the link text - not the link url, so there shouldn't be any problem.

    Not fine, since the link now refers to the wrong page.

    Unless you use a 302 .htaccess redirect.

Topic Closed

This topic has been closed to new replies.

About this Topic