Open Post Thumbnail in New Child Theme File in Wordpress

Today I am stuck in a new problem using WordPress Theme Development. What I'm going to do is to open a Thumbnail image in a new separate child theme, when the user clicks on the anchor tag.

Example: Click to Open Image

and it redirects the user to the image on a separate theme file.

Code Example for single.php

a href=PLZ MENTION CORRECT SYNTAX HERE ); ?link to Another Page /a

Child Theme File single-attachment.php

?php 
/***
Template Name: Advertisement
***/

get_header(); ?

?php echo get_the_post_thumbnail_url(); ?

?php get_sidebar(); ?
?php get_footer(); ?

Topic wp-load.php php plugin-development theme-development plugins Wordpress

Category Web


As mentioned in the comments, neither single-thumbnail.php or single-ad.php will work in a simple manner. Sure a complex solution/workaround could be written but it's totally unnecessary. The template hierarchy in WordPress doesn't work that way. Whatever follows single- in a single-XXXXX.php file is used to denote the content type/post type. So the single-ad.php would only be used by WordPress if it was attempting to display an ad post type.

Instead, make a duplicate of single-ad.php and name the copy single-attachment.php. Then go ahead formatting it however you like.

Now to get the url for your link you want to try the following:

<?php
    //First we get the ID of the Featured Image you attached to a post.
    $attachID       = get_post_thumbnail_id( $post->ID );
    //Then we use that ID to get the permalink/url.
    //Not the URL of image file itself but of the attachment post type.
    $attachLink     = get_permalink( $attachID, false );
?>
<!-- Finally we echo out the permalink into our HREF tag -->
<a href="<?php echo $attachLink; ?>">Link to the Attachment Post</a>

That should address everything you need.

Here are the links to the functions I used:

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.