Here is a bit of code to add to your functions.php which I think will do what you want. There are probably better ways to code this, but I believe this will work.
<?php // Shortcode is like [mm-link-to-post title='post title']
function mm_link_to_post_func($atts) {
extract(shortcode_atts(array(
'title' => '',
), $atts));
$output = '';
if ($title) {
global $wp_query, $wpdb;
$output = '';
$sql = "SELECT * from $wpdb->posts WHERE post_status='publish' AND post_title='$title'";
$myrow = $wpdb->get_row($sql);
if ($myrow) {
$link = $myrow->guid;
$output = "<a href='$link' title='Link to $title'>$title</a>";
}
}
return $output;
}
add_shortcode('mm-link-to-post', 'mm_link_to_post_func');
?>
Hi,
thanks a lot for this great code, works like a charm! I only have one small question, this solutions ignores permalinks and uses static links such as http://www.domain.com/?p=20, is there any way how to bypass this?
Thanks a lot!
Sorry, I could never get the_permalink() to work. Maybe someone else can help with that.
Hello,
Not a shortcode, but a greaaat plugin you should have a look to:
http://ww.wp.xz.cn/extend/plugins/link-to-post/
allows to include a link to a post (or a page) in 2 clicks only.
OK, I got it. If this works for you, please mark this topic ‘Resolved’.
<?php // [mm-link-to-post title='post title']
function mm_link_to_post_func($atts) {
extract(shortcode_atts(array(
'title' => '',
), $atts));
$output = '';
if ($title) {
global $wpdb;
$output = '';
$myid = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_title='$title' AND post_status='publish'");
if ($myid) {
$link = get_permalink(intval($myid[0]));
$output .= "<a href='$link' title='Link to $title'>$title</a>";
}
}
return $output;
}
add_shortcode('mm-link-to-post', 'mm_link_to_post_func');
?>