仅为访问者将帖子的特色图片链接到wordpress上的内部页面?

I want to link featured images of every wordpress posts automatically for only visitors. So it should not be available for logged-in users of wp. And it should only link featured images of single.php. Not for index.php, archive.php or others.

I have this code but do not know where (which file in wordpress) should I put this code and make it workable:

<?php
        $image = get_the_post_thumbnail_url( $post->ID, 'large' );
        $link  = is_user_logged_in() ? $image : 'https://example.com/';
    ?>

Maybe we can put this on functions.php or single.php? But when i put this it breaks the site or doing nothing.