• handig

    (@handig)


    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 https://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.

Viewing 8 replies - 1 through 8 (of 8 total)
  • vtxyzzy

    (@vtxyzzy)

    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.

    Thread Starter handig

    (@handig)

    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=”https://www.mywebsite.com/content/uploads/sun.jpg&#8221; 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.

    vtxyzzy

    (@vtxyzzy)

    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="https://mysite.com/this-page" title="nice day"><img class="alignleft" src="https://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:

    https://codex.www.ads-software.com/Shortcode_API

    Thread Starter handig

    (@handig)

    I need to study this, but it looks promising.

    Thanks for the help.

    Thread Starter handig

    (@handig)

    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.

    vtxyzzy

    (@vtxyzzy)

    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' );
    Thread Starter handig

    (@handig)

    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');
    Thread Starter handig

    (@handig)

    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?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Generic link to post itself’ is closed to new replies.