Permalink Shortcode on WordPress

Adapted from Added Permalinks Shortcode on WordPress.

function do_permalink($atts) {
	extract(shortcode_atts(array(
		'id' => 1,
		'text' => ''
    ), $atts));

    if ( '' == $text ) {
      $text = get_the_title($id);
    }

    return '<a href="' . get_permalink($id) . '">' . do_shortcode($text) . '</a>';

}
add_shortcode('permalink', 'do_permalink');

Usage

&#091;permalink id="140"]

will output: Permalink Shortcode on WordPress (as 140 is the ID of this post) which will always have the correct title.

Alternatively, specify the link text that won’t change:

&#091;permalink id="140" text="My custom link text"]

which output as:My custom link text.

Speak Your Mind

*