Forums

[resolved] using the_permalink to get the "ugly" url (11 posts)

  1. Boris Mahovac
    Member
    Posted 2 years ago #

    I'd like to add a "Share on Twitter" link to my blog posts.

    Since I'm using pretty permalinks the_permalink() returns a long url.

    Is there a way to continue to use pretty permalinks, but have the_permalink() return a short url, e.g. http://example.com/?p=N

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    You might want to have a look at some other Twitter plugins. Easy Retweet, for example, includes an option to enter a bit.ly API key (you'll need to get a free account first) - which means that all of your urls can be translated to a shortened bit.ly url.

  3. Boris Mahovac
    Member
    Posted 2 years ago #

    I know about plugins, but I'd like to use my own URL for SEO purposes. Also, what if bit.ly goes out of business? all my links would be useless.

  4. Mark / t31os
    Moderator
    Posted 2 years ago #

    Assuming you're inside the loop $post->guid will give you the regular style permalink for that post, eg. example.com/?p=1

  5. vtxyzzy
    Member
    Posted 2 years ago #

    Can you just use $post->guid instead of the_permalink()?

  6. Mark / t31os
    Moderator
    Posted 2 years ago #

    I don't see why not, do you have a differing opinion?.. :)

  7. vtxyzzy
    Member
    Posted 2 years ago #

    Sorry, I left for a while and should have refreshed the screen.

  8. ancawonka
    Member
    Posted 2 years ago #

    I'm doing some reasearch into using the guid value, and here's what it says on the http://codex.wordpress.org/Function_Reference/get_post page:

    guid
    (string) A link to the post. Note: One cannot rely upon the GUID to be the permalink (as it previously was in pre-2.5), Nor can you expect it to be a valid link to the post. It's merely a unique identifier, which so happens to be a link to the post at present.

    So, in the meantime, it seems like it's safe to use the GUID, but after a while it might not be...

    The cure for this is to keep up to date on upcoming changes in WP.

  9. jeanloui
    Member
    Posted 2 years ago #

    the_permalink() into the loop (e.g. archive.php of any theme) gives something like this:

    http://example.com/?p=124

    If I need to "insert" extra variables after the ID, e.g.:
    http://example.com/?p=124&mycategory=3&myclient=john
    I use this:

    $myurl = get_post($post, ARRAY_N);

    // this gives a numeric array of the class object, where $myurl[0] contains the ID of the post, even if we don't know if it is a "post" or a "page", but all my items are "posts"... and maybe $myurl[20] defines if it is a post or a page...

    Then I write manually the new url:

    <a href="?p=<? echo $myurl[0]; ?>&mycategory=<? echo $mycategory ?>&myclient=<? echo $myclient ?>">New URL</a>

    Note that after the <a href, there is only the question mark.

    hope this helps

  10. Mark / t31os
    Moderator
    Posted 2 years ago #

    You'd be better off using add_query_arg to add additional parameters to a URL.

    See:
    http://codex.wordpress.org/Function_Reference/add_query_arg

  11. jeanloui
    Member
    Posted 2 years ago #

    Thanks, t3, I will read carefully the codex directive, I'm a beginner, and you're a GREAT Moderator!!!

    Thank you for your time!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags