I am trying to create a custom slider in wordpress and add my own links but I am not able to get the featured image of a post to display. Here is my code that I am working with right now.
<?php $posts = get_posts('category=20&orderby=rand&numberposts=6'); foreach($posts as $post) { ?>
<?php echo '<li>'; ?>
<a href="<?php the_permalink() ?>" target="_parent"><?php the_title();?>
<?php wp_get_attachment_image( $attachment->ID, 'small' );?>
</a>
<?php echo '</li>'; ?>
This code works by displaying the links randomly but they never display the featured image of a post. Can someone please help out and let me know what i am doing wrong here...
Thanks
It is because the $attachment variable is null. I'd suggest to replace:
<?php wp_get_attachment_image( $attachment->ID, 'small' );?>
with the following instead:
<?php the_post_thumbnail( 'small' );?>
Related
So I tried to make a custom post type called 'showcase' and created a few posts in this custom post type. The problem is that when I click on a 'product' it should showup in a lightbox instead of going to the thumbnail url.
This is the code I am using:
<section id="showcase">
<?php
$args = array( 'post_type' => 'showcase', 'posts_per_page' => -1 ); $the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
$it = 0;
while ( $the_query->have_posts() ) : $the_query->the_post();
$terms = get_the_terms( $post->ID , 'showcase_category');
$it ++;
if ($it == 6) {
echo '</section><div class="cta-block"><h2>Live model drawing</h2><p>Drawing from a live model, gives you the opportunity to draw what you see instead of drawing what you think about.</p>View courses</div><section id="showcase">';
}
?>
<a class="entry-showcase" href="<?php the_post_thumbnail_url(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail( 'medium' ); ?>
</a>
<?php wp_reset_postdata();
endwhile; else :
endif; ?>
</section>
Also, I tried using a few plugins which automaticly detect jpg, gif etc but for some reason they are not working for thumbnail images.
Some plugins I tested:
Easy FancyBox,
Responsive Lightbox,
Simple Lightbox,
Also, I am not getting any errors it just goes straight to the media file in url instead of staying on the same page and showing the image in a lightbox.
Thanks in advance (:
You can maybe use the Fresco-Lightbox. After implementing you can add the "fresco"-class to any imagelink and open it in a lightbox:
<a class="entry-showcase fresco" href="<?php the_post_thumbnail_url(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail( 'medium' ); ?>
</a>
Also see the Fresco documentation.
On my static front page I have created, I query the Wordpress Loop to only show the two most recent blog posts. For some reason, it started displaying two instances of the attached image for each post. Here is what it looks like:
I have no idea why it is outputting two of the attached images for each.
Here is the code for this specific part of front-page.php:
<div class="row">
<?php $latest = new WP_Query('showposts=2'); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
<a href='<?php echo get_permalink(); ?>'>
<div class="col-md-6 blog-wrap">
<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' ); ?>
<div class="home-featured-img" style="background: url(<?php echo $src[0];?>);"></div>
<div class="excerpt-home">
<?php get_template_part( 'content', get_post_format() ); ?>
</div>
</div></a>
<?php endwhile; ?>
</div>
I have a feeling that the problem occurs when I call:
If you need any other code snippets from my custom theme, please let me know.
I only want for the post to display the image from this (which is the top picture):
<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' ); ?>
<div class="home-featured-img" style="background: url(<?php echo $src[0];?>);"></div>
Any and all suggestions are greatly appreciated.
you can remove any code like that
<?php
// Post thumbnail.
twentyfifteen_post_thumbnail();
?>
Explanation...
that file display the second image - the title - the meta
you need to edit it to remove the code that display the image :) thats it.
I am trying to add the featured image url to the featured image, so it will open up in a lightbox when clicked.
My code is:
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail();} ?>
<?php endwhile; ?>
<?php else: ?>
<article>
<h1><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h1>
</article><!--/ Article -->
<?php endif; ?>
I have tried quite a few things but cannot get it to pull the thumbnail URL. I have tried adding these to where the link # is:
wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ));
...
$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) );
...
<?php echo $thumbnailsrc ?>
I am sure there is a simple solution that I am yet to find. Thanks to anyone who maybe able to assist :-)
please try this code adding in to your post loop. and confirm you have set feature image for that post which you listings.
<?php $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' ); ?>
<img src="<?php echo $img_ar[0];?>" />
<?php } ?>
Try this:
wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' )[0]
Assuming you are in The Loop, you can simply use get_post_thumbnail_id() to get the current post featured image ID. You also need to pass the second parameter full in order to get the non-resized version of the image for your lightbox source. You can limit it to large as well, if you like.
I am trying to filter posts from category.
This category is called "images"
And I have the code below:
<div id="owl-images" class="owl-carousel">
<?php query_posts('category_name=images'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 5600,1000 ), false, '' );?>
<a href='<?php echo $src[0]; ?>' data-lightbox="image-77" data-title="<?php the_title() ?>">
<div class="item">
<div class="lazyOwl" style="background:url(<?php echo $src[0]; ?>) no-repeat center center;background-size:cover;height:300px;" alt=""></div>
</div>
</a>
<?php endwhile; endif; ?>
</div>
The problem is that:
It is displaying some posts from other categories
It is not displaying all posts from category "image"
What is wrong with the code?
PS: I have seen this answer and it didn't help. Remained the same.
Best regards.
You should try with
cat=cat_id
instead of
category_name
For change Your default show posts per page:
Go to setting -> reading -> Blog pages
try like this
query_posts( array ( 'category_name' => 'images' ) );
you need to pass the slug of category image in this case.
You can also try with this as well
$catIDs = get_cat_ID('images');
query_posts("cat=$catIDs");
Use category__in and pass your category id
<?php query_posts('category__in=cat_id'); ?>
I need to be able to display featured images (category 4 in my wordpress site) on a separate page just the images not the rest of the post.
I created a template named 'ban' and attached it to a page named banners in which to display the category 4 images.
I was able to list the categories but that is as far as I got:
<?php wp_list_categories('include=4') ?>
I'm not sure how to go about doing this, if someone could explain it to me it would be greatly appreciated!
If you want to get all featured images from the posts under category 4 then this might be the solution
$the_query = new WP_Query();
$the_query->query("cat=4&nopaging=true");
if ($the_query->have_posts()) :
while($the_query->have_posts()) : $the_query->the_post();
if(has_post_thumbnail()) :
the_post_thumbnail();
endif;
endwhile;
endif;
wp_reset_postdata();
If you want to get the real width/height of then images you can use this:
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
?>
<img src='<?php echo $image[0]; ?>' />
<?php
Just replace the the_post_thumbnail() function. Hope that helps!