Support » Fixing WordPress » How do I hyperlink between posts?

  • Hi:

    I just begun using WordPress last Saturday. I love it!

    How do create hyperlinks between my blog posts that would not break in case I switch hosts or directories or someone mirrors my content? Or must I code as href the full http url.

    My blog entries in the last four days desperately needs cross-linkage, e.g., “As I discussed in ‘eHarmony FAQ’ last Saturday, there are many….”

    Regards,

    eHarmony Blog

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

    (@otto42)

    WordPress.org Admin

    Relative permalinks from the root directory.

    If your post’s permalink looks like this:
    http:/example.com/blog/some/stuff/here/storyname

    then you link to this href:
    /blog/some/stuff/here/storyname

    This won’t help if you switch directories around, or switch your permalinks around, but there’s no way to help that anyway. This is the best compromise, since it will be fine if you switch domain names, but will actually work from all points on your site.

    Thread Starter eharmonyblog

    (@eharmonyblog)

    I see. Thanks. Wouldn’t it be grand if I can just code say (a href=”ZZZ”)text(/a) in the post editor, where ZZZ is the post-slug or the post-ID, and WordPress will automatically convert it to a blog-independent, host-independent URL? This means I don’t embed my blog’s file structures in my content, which I thought was important.

    Wasn’t this requested before? As a plugin, perhaps?

    eHarmony Blog

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    If you have any kind of Exec PHP plugin, that lets you put PHP in your posts, then you could use the get_permalink($id) function to get the link to any given post by ID.

    It would be possible to create a quicktag for this sort of thing using a plugin. Hmm… Gimme a while to think about it.

    Edit: Okay, that was a while. Here you go:
    http://ottodestruct.com/wpstuff/postlink.zip

    It’s a plugin. Install it, activate it. You can then use tags of the form:
    [ID=###]Link to my post[/ID]
    In your posts. The ### should be the number of the post ID you want to link to. This will be replaced by an a href permalink. If your permalinks change or your blog moves or whatever, these will change too, as long as you have the plugin working.

    Downside: If you disable the plugin, they all change back to the [ID=] stuff. The replacement happens at display time, not at post time.

    Thread Starter eharmonyblog

    (@eharmonyblog)

    Otto, the file activated successfully, but apparently doesn’t do anything. [ID=1]Link to my post[/ID] stays that way.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Doh. I had it one way, then changed it, and forgot to test that change.

    Change the two lines inside the foreach() block to these:

    $text = str_replace("[ID=".$val[0]."]","<a href=\"".get_permalink($val[0])."\">",$text);
    $text = str_replace("[/ID]","</a>",$text);

    That will fix it.

    Thread Starter eharmonyblog

    (@eharmonyblog)

    Finally! — blog-independent, platform-independent, webhost-independent hyperlinking between posts of one blog. It worked in excerpts, posts, RSS feeds and even “email this post”‘s. THANK YOU!

    I think this needs to be put in WordPress’s plugin repository or whatever-it’s-called. Or, at the least, this feature be added to WordPress.

    eHarmony Blog

    Thread Starter eharmonyblog

    (@eharmonyblog)

    Oh Otto I’m sorry, but, apparently, only the first [ID] pair is converted. May I trouble you and ask your php wizardry again?

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Whoops. preg_match_all doesn’t work with foreach() in quite the way I thought it did.

    Anyway, problem fixed. Download it again, same link as before. Just overwrite the old version with the new one.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How do I hyperlink between posts?’ is closed to new replies.