I have a function that is hooked to the end all posts. I’m adding a shortcode so the same function can be called else where. And I need to change the output method based on whether the function was called by a shortcode or the hook.
I have this:
if (did_action(‘hd_related_posts’) > 0) {
// Function was called by the ‘hd_related_posts_shortcode’ shortcode
return ob_get_clean();
} else {
echo” Function was not called by the shortcode”;
echo ob_get_clean();
}
full script posted at https://pastebin.com/nu6XvzgJ
But even when I call the function from the shortcode, this IF statement returns false and the output is echoed when it should be returned. How do I fix this?
[ad_2]
We need to see all the code – you haven’t included enough for us to know why it isn’t working.
Keep in mind, a function is supposed to only do one job. So make another to handle your shortcode. You can execute the hook function within, if it’s necessary.
Can you not just add an parameter like `shortcode = false` to the function and only pass true if it’s invoked via the short code, or something similar ?