I will try to explain my question as good as possible. but since english is not my main language I hope you will understand.
Im trying to fill a carrousel with a custom post type. if got everything working except a read more button. which will take you to the single-post.
Now I'm trying to add it through a short code that I can add to the place where I load the metadata (this is from the backend of WordPress and not via a php file on the server..)
The only problem with my current setup is that I have to fill in the id every time I make a post like: [permalink id=131]
I would like to have a short code that also gets the id from the db. so that my short code will only be [permalink]
. So I can add it once to the loop.
Atm I'm using the following code in my functions.php
:
function my_permalink_shortcode( $atts ) {
$id = intval($atts['id']);
if ($id <= 0) { return; }
// Check that $id has a URL.
$url = get_permalink($id);
if ($url == '') { return; }
// Get link option and title.
$link = ($atts['link'] == '1') ? true : false;
$title = (trim($atts['title']) == '') ? get_the_title($id) : $atts['title'];
// Determine if we create a link.
if ($link) {
return '<a href="'.$url.'">'.$title.'</a>';
} else {
return $url;
}
}
add_shortcode('permalink', 'my_permalink_shortcode');
Thanks for reading. Kind regards. Mark
return;
withreturn '';
. I suspect all you need to do is removeif ($id <= 0) { return; }
as a value of0
will be interpreted as meaning the current postif ($id <= 0) { return; }
line. But i still need to input the id in the shortcode, otherwise it will return a link of the current page.