Insert featured image in single.php
Hi, how can I make the featured image show in the file single.php?
We know you can do with the file functions.php but we would like to insert the featured image in single.php.
I think this functionality should be built into the plugin, since it automatically pulls the featured image into the schema markup. According to the AMP docs:
Some platforms that use AMP HTML [require that] the leader image referenced in the markup must appear somewhere on the AMP HTML document itself.
But for now, this would get you the url, width, and height of featured image: wp_get_attachment_image_src( get_post_thumbnail_id( $this->get( 'post_id' ) ), 'full' )
Thanks for the reply @smhmic. And how do I enter in the file single.php?
This is my single.php:
<!doctype html>
<html amp>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://fonts.googleapis.com/css?family=Merriweather:400,400italic,700,700italic|Open+Sans:400,700,400italic,700italic" rel="stylesheet" type="text/css">
<?php do_action( 'amp_post_template_head', $this ); ?>
<style amp-custom>
<?php $this->load_parts( array( 'style' ) ); ?>
<?php do_action( 'amp_post_template_css', $this ); ?>
</style>
</head>
<body>
<nav class="amp-we-title-bar">
<div>
<a href="<?php echo esc_url( $this->get( 'home_url' ) ); ?>">
<?php $site_icon_url = $this->get( 'site_icon_url' ); ?>
<?php if ( $site_icon_url ) : ?>
<amp-img src="<?php echo esc_url( $site_icon_url ); ?>" width="32" height="32" class="amp-wp-site-icon"></amp-img>
<?php endif; ?>
<?php echo esc_html( $this->get( 'blog_name' ) ); ?>
</a>
</div>
</nav>
<div class="amp-wp-content">
<h1 class="amp-wp-title"><?php echo esc_html( $this->get( 'post_title' ) ); ?></h1>
<ul class="amp-wp-meta">
<?php $this->load_parts( apply_filters( 'amp_post_template_meta_parts', array( 'meta-author', 'meta-time', 'meta-taxonomy' ) ) ); ?>
</ul>
<div class="amp-ad">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- OverPress Reattiva -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXXXXXXXXXX"
data-ad-slot="XXXXXXXXXXX"
data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
<?php echo $this->get( 'post_amp_content' ); // amphtml content; no uses ?>
</div>
<div class="amp-ad">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- OverPress Reattiva -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-XXXXXXXXXXXX"
data-ad-slot="XXXXXXXXX"
data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
<nav class="amp-we-title-bar-footer">
<div>
<a href="<?php echo esc_url( $this->get( 'home_url' ) ); ?>">
<?php $site_icon_url = $this->get( 'site_icon_url' ); ?>
<?php if ( $site_icon_url ) : ?>
<amp-img src="<?php echo esc_url( $site_icon_url ); ?>" width="32" height="32" class="amp-wp-site-icon"></amp-img>
<?php endif; ?>
<?php echo esc_html( $this->get( 'blog_name' ) ); ?>
</a>
</div>
</nav>
</body>
</html>
Seconding adding this into the plugin itself, at least as a selectable option for those who want it.
I did not understand :(
I added a fallback to my AMP pages, if no image was available. Used the featured image example and used wp_get_attachment_image( $attchament_id_of_fallback_image, 1280) ) to link the image.
I than extended the amp_post_template_metadata filter to include that file, which makes sure the Microformat data is correct as well.
` if(empty($metadata['image'])){
$metadata['image'] = array(
'@type' => 'ImageObject',
'url' => wp_get_attachment_url($attchament_id_of_fallback_image),
'height' => 160,
'width' => 1280,
);
}`
All pages validating fine now :)
Dupe #327