WordPress.org

Ready to get started?Download WordPress

Forums

Generic link to post itself (9 posts)

  1. handig
    Member
    Posted 2 years ago #

    Hi,
    Is there a way to link to a post itself? I'm looking for a generic link?

    So for example if the post url is http://www.mywebsite.com/newpost/

    I could link to the website by html:
    <a href="/">Link to website</a>

    I know I can do
    <a href="/newpost/">Link to website</a>

    But was wondering if there was something you could use so you don't need to have to type the postname and have WordPress do it.

  2. vtxyzzy
    Member
    Posted 2 years ago #

    A lot depends on where you want the link. If it is in The Loop, or if you have the post ID, you could write a function to return the permalink of the post, either in a shortcode, or in a template.

    Please explain more about what you want to do, preferably with an example.

  3. handig
    Member
    Posted 2 years ago #

    It is in the loop.

    I'm using excel to create posts in a specific template.

    So I would like to have the image linked to the post without needing to copy the post url after I published the post.

    So for example I have a post Nice day.
    So I just type in a column of excel Nice day and then Excel produces in another column something like this:

    <img class="alignleft" src="http://www.mywebsite.com/content/uploads/sun.jpg" title="nice day" alt="nice day" />

    So then I just replace the image url and at the moment I need to copy also the post url.

  4. vtxyzzy
    Member
    Posted 2 years ago #

    I can't be sure, but I think you could use a shortcode.

    It has been a LONG time since I worked with Excel macros, so bear with me. I assume that you can separate 2 comma separated values in a cell into the 2 parts.

    What I am suggesting is that you put the comma separated values for the title/alt and the relative path to the image, something like this, in the Excel column: Nice Day,sun.jpg

    Excel would generate: [post_img img="sun.jpg" alt="nice day"]

    Then you would code a function (in functions.php) to process the post_img shortcode and return this:

    <a href="http://mysite.com/this-page" title="nice day"><img class="alignleft" src="http://mysite.com/content/uploads/sun.jpg" alt="nice day" title="nice day"></a>

    The href can be obtained using the get_permalink() function.

    The uploads path can be obtained using the wp_upload_dir() function.

    Here is a Codex article on using shortcodes:

    http://codex.wordpress.org/Shortcode_API

  5. handig
    Member
    Posted 2 years ago #

    I need to study this, but it looks promising.

    Thanks for the help.

  6. handig
    Member
    Posted 2 years ago #

    Hi,
    I'm trying to grasp how it works.

    I tried this to return only the permalink via a shortcode but get an error:
    //[permalink]
    function permalink_funct( $atts ){
    return get_permalink();
    }
    add_shortcode( 'permalink, 'permalink_func' );

    If you could give me a hint.

  7. vtxyzzy
    Member
    Posted 2 years ago #

    If you posted the actual code you used, it looks like there is a missing quote in this line:

    add_shortcode( 'permalink, 'permalink_func' );

    It should be this:

    add_shortcode( 'permalink', 'permalink_func' );
  8. handig
    Member
    Posted 2 years ago #

    Hi,
    Thanks but didn't work. I found this code here.

    When I use the shortcode without id it returns the permalink from the current post. So I use[permalink] with the following code:

    function permalink_thingy($atts) {
    	extract(shortcode_atts(array(
    		'id' => 1,
    		'text' => ""  // default value if none supplied
        ), $atts));
    
        if ($text) {
            $url = get_permalink($id);
            return "<a href='$url'>$text</a>";
        } else {
    	   return get_permalink($id);
    	}
    }
    add_shortcode('permalink', 'permalink_thingy');
  9. handig
    Member
    Posted 2 years ago #

    Hi,
    I noticed that if I show it on a category page the permalink shown by the shortcode is the category page url instead of the post url.

    How can I get the post url when displayed on a category page?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags