0

i would like to include an affiliate link as a widget which inserts the title of the respective page as search parameter.

<a href="www.amazon.com/s?=titleofthepage">Link</a>

I have already created a shortcode in the functions.php with get_the_title

function post_title_shortcode(){
$variable = get_the_title();
}
add_shortcode('post_title','post_title_shortcode'); 

and it is called [post_title]

However

<a href="www.amazon.de/s?=[post_title]"></a>

didn't work. Does anyone have any other idea how to make this happen?

0

Your shortcode is getting the title, but you haven't told it to return anything, so a small tweak should fix things:

<?php
function post_title_shortcode(){
    return get_the_title();
}
add_shortcode('post_title','post_title_shortcode');
?>

You could also continue setting get_the_title() to $variable and add a line to return $variable, but the above is the simplest, shortest option.

| improve this answer | |
  • Also note, shortcodes may not work in widgets at all if you haven't used code to enable it, so it may be worth editing the widget itself to output the title instead of adding a shortcode into the mix. – WebElaine May 27 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.