I found a simple code that gets the featured image from a post.
<?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 1680,470 ), false, '' );
echo $src[0];
?>
I need this for a page that uses a image from a category "slider" and sets featured image. This will make a header image on the page.
<?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 1680,470 ), false, '' );
?>
<div id="header-hero" style="background-image: url('<?php echo $src[0]; ?>');">
But if someone makes a new post in another category it fails. So, how can I get the image from the category? It will be only one image in the category so it makes it a little easier. Hope for some wordpress-gurus :)
Use WP_query, with parameters in array.
$args = array(
'category_name'=>'your-category-slug',
'posts_per_page'=> 10,
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
//Post data
echo get_the_post_thumbnail(get_the_ID());
endwhile;
Try this:
<?php
$slider_category_id = 123213;
query_posts('showposts=1&cat='.$slider_category_id);
if (have_posts()) : while (have_posts()) : the_post();
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 1680,470 ), false, '' );
?>
<div id="header-hero" style="background-image: url('<?php echo $src[0]; ?>');">
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
Related
I am pulling posts from a specific category. This feature works. However, I also need it to pull pages every now and then and I can't figure out a way to get it to pull this in addition to the posts.
HTML/PHP
<?php query_posts('cat=8&posts_per_page=3'); while (have_posts()) : the_post(); ?>
<div>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" class="card-img img-responsive">
<p class="feature-card-head"><?php the_title(); ?></p>
<p class="feature-card-txt"><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I'd like to keep this structure and find a way to include pages, any feedback is helpful!
You cannot fetch posts and use this query as it is. WordPress posts have no categories by default so the cat=x part will always exclude automatically all posts.
I think there is no better solution than using a second query. Depending on what you are using this for, it may be better to separate these loops.
If you want to use a single loop for multiple queries consider merging the query like mentioned here:
<?php
$query1 = new WP_Query(array(
'cat' => 8,
'post_type' => 'post',
'posts_per_page' => 3
));
$query2 = new WP_Query(array(
'post_type' => 'page',
));
$wp_query = new WP_Query();
$wp_query->posts = array_merge( $query1->posts, $query2->posts );
$wp_query->post_count = $query1->post_count + $query2->post_count;
?>
<?php while( $wp_query->have_posts() ): $wp_query->the_post(); ?>
<div>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" class="card-img img-responsive">
<p class="feature-card-head"><?php the_title(); ?></p>
<p class="feature-card-txt"><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; wp_reset_query(); ?>
I am using wordpress and trying to create a blog landing page to show the most recent blog posts. So far so good but I am having difficulties showing the blog image in the image tag. I am able to obtain the postId by using get_the_id function. I was also able to get the date of the post by using the_date function.
However, I cannot get the wp_get_attachment_image function to show the image of the blog post.
Please see my code below.
<?php $query = new WP_Query( 'posts_per_page=5' ); ?>
<?php while ($query -> have_posts()) : $query -> the_post(); ?>
<div class="blog">
<img src="wp_get_attachment_image( get_the_ID() ); ">
<h3><?php the_title(); ?></h3>
<p><?php the_date(); ?></p>
<p><?php the_excerpt(__('(more…)')); ?></p>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
Use following code for get the attached image.
<img src="<?php echo wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');?>">
The function wp_get_attachment_image is waiting for attachement_id.
See below in wp, how we get all attachments from a post :
<?php if ( $post->post_type == 'data-design' && $post->post_status == 'publish' ) {
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
) );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
echo '<li class="' . $class . ' data-design-thumbnail">' . $thumbimg . '</li>';
}
}
}
?>
I had to use the following function the_post_thumbnail() and echo the result within an image tag
<img src="<?php echo the_post_thumbnail();?>">
The following code worked.
<?php $query = new WP_Query( 'posts_per_page=5' ); ?>
<?php while ($query -> have_posts()) : $query -> the_post(); ?>
<div class="blog">
<img src="wp_get_attachment_image( get_the_ID() ); ">
<h3><?php the_title(); ?></h3>
<p><?php the_date(); ?></p>
<p><?php the_excerpt(__('(more…)')); ?></p>
</div>
?>
I have some problem. I use Wordpress theme - Intergalatic. But in homepage, the large background of post do not take us to the post. The background must be clickable and move us to the post. How to do it?
This is code of thumbnail.
<?php if ( has_post_thumbnail() ) {
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'intergalactic-large' ); ?>
<div class="entry-background" style="background-image:url(<?php echo esc_url( $thumbnail[0] ); ?>)"></div>
<?php } ?>
Assuming you're in the loop, and by the looks of it you are, then you'll need to change div to anchor
<?php if ( has_post_thumbnail() ) {
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'intergalactic-large' ); ?>
<?php } ?>
And probably change the anchor to display:inline-background; and possibly add the height of the image to the styling like:
<?php if ( has_post_thumbnail() ) {
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'intergalactic-large' ); ?>
<?php } ?>
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; ?>
For some reason I cannot get attachment to display if i pass in $attachment_id if i pass a real value in like 187 it works.
I am using WpAlchemy and Custom Image Sizes plugin.
<section id="new">
<?php $myquery = new WP_Query(array('post_type' => array('post', 'website_gallery'),'showposts' => '2'));
while ($myquery->have_posts()) : $myquery->the_post();
global $custom_metabox;
?>
<div class="latest hentry">
<h2><?php the_title(); ?></h2>
<?php $website_gallery->the_meta(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php $website_gallery->the_value('galleryimage');?>" alt="<?php the_title(); ?>">
</a>
<?php echo wp_get_attachment_image($attachment_id, '220x80'); ?>
</div>
<?php endwhile; wp_reset_query(); ?>
</section>
I think you can get the attachment id by using the get_post() function. get_post() requires an array for it's parameter and that array can be something like this:
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
Since you want attachments, make sure your 'post_type' => 'attachment'.
You can then do:
$attachments = get_post( $args );
if( $attachments ) {
foreach( $attachments as $attachment ) {
echo wp_get_attachment_image( $attachment->ID, '220x80' );
}
}
Adapt that code for what you need. Hopefully it'll work for you.
Check out: http://codex.wordpress.org/Template_Tags/get_posts#Show_attachments_for_the_current_post