I have a website that i'm working on where I need to generate a unique image in header.php for each individual post.
I have this in my index.php:
$pageposts = $wpdb->get_results($querystr, OBJECT);
foreach($pageposts as $ppost){
$ID = $ppost->post_id;
$title = get_the_title( $ID );
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $ID ), 'single_post_thumbnail' );
$t = $thumb[0];
$desc = get_post($ID)->post_content;
$out = get_post_meta($ID,'outbound',true);
$output = "
<div class='ph-sticky' id='phf'>
<span class='icon-x'><i class='fa fa-times icon-xy'></i></span>
<div class='row hunt-row-fp'>
<a class='title' href='$out' target='_blank' rel='nofollow'>$title</a>
<div class='img-featured'><img class='phsi' src='$t'/></div>
<span class='description'>$desc</span>
</div>
</div>";
}
echo $output;
wp_reset_query();
?>
This correctly generates a thumbnail for each post, however in my header.php my code only generates the image of the FIRST post on the page. Here it is:
<!-- post modal -->
<div class="show-post-modal">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' ); ?>
<div class="comments-bg-image" style="background-image: url('<?php echo $image[0]; ?>')" >
</div>
<?php endif; ?>
<div class="comments-header">
I've tried multiple solutions however I can only seem to pull in the first thumbnail, which becomes the background for all posts. Any help would be most appreciated!
i think this will help you a lot.
<?php
$page = get_post($post->ID);
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
if(!empty($post_thumbnail_id)) {
$img_ar = wp_get_attachment_image_src( $post_thumbnail_id, 'full' );?>
<style>
.comments-bg-image{ background-image: url(<?php echo $img_ar[0];?>); }
</style>
<?php } ?>
Related
I have a loop that gets the title, content, and link of all pages that are a child of the current page. Everything is working except I don't know how to get the featured image url for these pages so that I can use it as a background image. You can see in the code where I have commented where I am trying to get the url of the featured image.
Here is my code:
<?php
$mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'menu_order', 'sort_order' => 'asc' ) );
foreach( $mypages as $page ) {
$content = $page->post_content;
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
$trimmed_content = wp_trim_words( $content, 14, "..." );
?>
<div class="service-box block-grid-item">
<div class="row">
<div class="col-xs-6 col service-image">
<!-- This is where I am trying to get the featured image url -->
<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<a href="<?php echo get_page_link( $page->ID ); ?>" style="background-image: url(<?php echo '$feat_image'; ?>)"</a>
</div>
<div class="col-xs-6 col service-body">
<a href="<?php echo get_page_link( $page->ID ); ?>">
<span class="service-title"><?php echo $page->post_title; ?></span>
<span class="service-text"><?php echo $trimmed_content; ?></span>
<span class="service-link">Read more</span>
</a>
</div>
</div>
</div>
<?php }
?>
You're close, but you should be using wp_get_attachment_image_src(), and should remove the quotes from your variable when you echo the source:
$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
<a href="<?php echo get_permalink( $page->ID ); ?>"
style="background-image: url(<?php echo $feat_image; ?>)">
You should also take a look at your HTML markup and make sure it's valid.
on above answer some part of code is missing. here is the complete code.
<?php $feat_image = wp_get_attachment_image_src( get_post_thumbnail_id( 5 ), 'large' );
print_r($feat_image);
?>
<a href="<?php echo get_permalink( '5' ); ?>"
style="background-image: url(<?php echo $feat_image[0]; ?>)"> </a>
I am trying to show a small version of the featured image as a post thumbnail on my main page (index.php). For this I am implementing it as the background-image of a div. Unfortunately the code (which had been working before) has stopped working now and I cannot find a reason why. I have been looking everywhere for a solution, but I just cant figure it out.
I am using the following code but unfortunately it doesnt return anything:
<?php $post_image_id = get_post_thumbnail_id($post_to_use->ID);
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'post-thumbnail', false);
if ($thumbnail) (string)$thumbnail = $thumbnail[0];
} ?>
<?php if (has_post_thumbnail()) : ?>
<div class="post_image_crop" style="background: url('<?php echo $thumbnail; ?>')">
</div>
<?php endif; ?>
You can have a look at it here: oliverprenzel.com
The weird thing is, I have done exactly the same thing on my single.php where it still does work.
<?php $post_image_id = get_post_thumbnail_id($post_to_use->ID);
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'post-thumbnail', false);
if ($thumbnail) (string)$thumbnail = $thumbnail[0];
} ?>
<?php if (has_post_thumbnail()) : ?>
<div class="post_image_bg" style="background: url('<?php echo $thumbnail; ?>'); background-size: 100% !important;">
</div>
<?php endif; ?>
You can have a look at it working here: oliverprenzel.com/headmagazine/
Does anyone have an idea what might be causing this?
Edit for Claudiu:
This is the loop I am trying to get the image in:
<div class="wrapper">
<?php $post_image_id = get_post_thumbnail_id( $post_id );
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'post-thumbnail', false);
if ($thumbnail) (string)$thumbnail = $thumbnail[0];
} ?>
<!-- post loop prev -->
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>">
<div class="post_frame">
<div class="post_image_crop" style="background: url('<?php echo $thumbnail; ?>')">
</div>
<div class="prev_det">
<div class="prev_det_center">
<h1>
<?php the_title(); ?>
</h1>
<p><?php echo get_the_category_list(', '); ?></p>
</div>
</div>
<div class="prev_det_fold"></div>
</div>
</a>
<?php endwhile; ?>
<?php else: ?>
<?php endif; ?>
</div>
If you want to get each posts thumbnail, then you need to move
<?php $post_image_id = get_post_thumbnail_id( $post_id );
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'post-thumbnail', false);
if ($thumbnail) (string)$thumbnail = $thumbnail[0];
} ?>
inside the loop and change it to:
<?php $post_image_id = get_post_thumbnail_id( get_the_ID() );
if ($post_image_id) {
$thumbnail = wp_get_attachment_image_src( $post_image_id, 'post-thumbnail', false);
if ($thumbnail) (string)$thumbnail = $thumbnail[0];
} ?>
My guess is that $post_image_id is returning null because you don't have a featured image for your home page and even if you had, the same image will be displayed for each post.
I have a strong suspicion that in your first line $post_to_use->ID returns null. When the argument in get_post_thumbnail_id() returns null then the current post id is used by default. This is why it is working in single.php, because there you have a post loaded.
On frontpage you don't. So you have to check which post id you want, manually enter it or do a loop.
Try replacing get_post_thumbnail_id($post_to_use->ID) with get_post_thumbnail_id(5) where 5 is the post id that you want.
How to assign links to thumbnail images. I have portfolio with thumbnail images of different websites and i have to add link to them.
<div class="showcase-image">
<?php
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
if(has_post_thumbnail()): echo '<img src="'.$thumbnail[0].'"/>'; else: ?>
<?php endif; ?>
</div>
Try this
<div class="showcase-image">
<?php
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
if(has_post_thumbnail()): echo '<a href="'.$thumbnail[0].'" > <img src="'.$thumbnail[0].'"/></a>'; else: ?>
<?php endif; ?>
</div>
How can I set the featured images on all my posts to be outputted as a background image to a div. For example
<div class="postimg" style="background-image:url('https://s3.amazonaws.com/ooomf-com-files/8jLdwLg6TLKIQfJcZgDb_Freedom_5.jpg')"></div>
Currently the featured image is being outputted as a regular image using this helper <?php the_post_thumbnail( 'wpbs-featured' ); ?>
I would suggest simply something along the lines of this
Get post featured image URL and echo it out accordingly:
<?php
$img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "full");
$img = $img[0];
?>
<div class="postimg" style="<?php if($img){echo 'background:url('.$img.');';} ?>">
</div>
Use this
<div style="background-image:url('<?php echo wp_get_attachment_url( get_post_thumbnail_id() );?>')"></div>
Try this...
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div id="custom-bg" style="background-image: url('<?php echo $image[0]; ?>')">
</div>
<?php endif; ?>
This is a wordpress related question. I want to turn an image that was submitted via frontend submission form where you provide the external url into an image that links to its own source. For ex. http://i.imgur.com/7qmFBPa.jpg would be the end result of clicking on the image link. Heres what the php looks like at the moment:
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<?php if ( has_post_thumbnail() ) { ?>
<div class = 'span4 reddit-image-single pull-left'>
<img src = "<?php echo $image[0]; ?>" width = "700px" class="img-rounded">
</div>
<?php }else{ ?>
<div class = 'span4 reddit-image-single pull-left'>
<img src = "<?php echo get_post_meta( $post->ID, 'wpedditimage', true ); ?>" width = "700px" class="img-rounded">
</div>
<?php } ?>
how do i go about modifying the above code? I'm fairly new to PHP.
wrap the image in tags with the url as the href
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<?php if ( has_post_thumbnail() ) { ?>
<div class = 'span4 reddit-image-single pull-left'>
<img src = "<?php echo $image[0]; ?>" width = "700px" class="img-rounded">
</div>
<?php }else{ ?>
<div class = 'span4 reddit-image-single pull-left'>
<img src = "<?php echo get_post_meta( $post->ID, 'wpedditimage', true ); ?>" width = "700px" class="img-rounded">
</div>
<?php } ?>
This is the main line you want to look at
<img src = "<?php echo $image[0]; ?>" width = "700px" class="img-rounded">