add_action('wp_head', 'my_favicon');
function my_favicon() {
echo "<link rel="shortcut icon" href="https://wordpress.stackexchange.com/questions/383392/" . get_stylesheet_directory_uri() .
"/images/favicon.ico">";
}
I think the problem was incorrect quotation marks on the string.
Can you try this code.