I am having trouble getting the image attachment to show by the title & excerpt on a Wordpress page in a list. I am using a new WP_Query to generate the list, consisting of 3 posts, from certain categories. Inside the loop, I'm trying to get the first image of each post, retrieving their image IDs with a 'get_children' array (the posts do not have featured images, so I can't use the_post_thumbnail). Using console.log, I can see that I am at least getting the image url for the first item in the list, but not for the others. And even with the one I am getting, I can't get it to display using wp_get_attachment_thumb_file(). Here's the code I have so far:
<div class="inner-left">
<ul class="blog-posts-ul">
<?php
global $wp_query;
$wp_query = new WP_Query(array('order' => 'DESC', 'posts_per_page' => 3, 'cat' => '21,23,689,741,1589'));
if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<?php $attachment = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image' ) );
if ( $attachment ) {
$attachment = current( $attachment );
$image_url = wp_get_attachment_thumb_url( $attachment->ID );
$attachment_id = attachment_url_to_postid( $image_url );
echo '<script>console.log("thumb: "' . $image_url . ')</script>';
}; ?>
<?php the_title(); ?><?php the_excerpt(); ?>
<?php wp_get_attachment_thumb_file( $attachment->ID ); ?>
</li>
<?php endwhile; endif; ?>
</ul>
<?php // Reset Query
wp_reset_query(); ?>
</div><!-- inner-left -->
The test site is at http://testsite.humortimes.com/ if you want to have a look. Scroll down, it's formatted as 3 squares, below the 'Latest Humor Times Faux News Headlines' section. Right now, it's not formatted very well, I plan on having two columns, there's just one showing.
Any help would be appreciated. Thank you.
Stop using console log and start using var_dump ($image_url) ; that will print whatever you have directly on that element.
I dont quite get why you cant use post thumbnail?
After a lot of frustration, I found this nifty function that does just what I want. It queries the database to find the first image of the post, which I guess is the key I was missing. It also tests for a featured image first, so that's taken care of as well. I put the function in my child function file, and just call it when needed on a page.
If you use it, be aware you need to add $size = 'thumbnail'; because, although her description says it'll return a thumbnail, it was returning full size images. This variable is used throughout the function, though, so you just need to give it a value.
Thanks, Amberweinberg!
Related
I need to edit my Wordpress child theme (based on Twenty Twenty One) archives.php template so that it grabs the first image added within any Post, not the Featured Image as that is being used by another plugin (and as such has to be different dimensions than what we want here). I've tried a few different solutions but nothing seems to work, I am guessing this may be to do with the fact I am using Gutenberg blocks.
I did however get the following code to work for the posts I had already uploaded (I have set out the most relevant line of code between two spaces within the archiveContainer div), but bizarrely after then creating some new test posts the code is no longer working and is just grabbing the Featured Images again, which is not what we want.
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php $attachment = array_values( get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'numberposts' => 1 ) ) ); ?>
<h2 class="archiveTitle" id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</h2>
<div class="archiveContainer">
<?php if( $attachment ) echo '<img src="' . wp_get_attachment_url($attachment[0]->ID) . '" class="post-attachment" />'; ?>
</div>
<?php get_template_part( 'template-parts/content/content', get_theme_mod( 'display_excerpt_or_full_post', 'excerpt' ) ); ?>
<?php endwhile; ?>
It could well be that I have inadvertently changed or deleted something of course - but, any ideas how I can get this working consistently?
I am looking to show the Wordpress featured images of blog posts alongside the snippets and titles of the blog posts (already there). We don't normally work in PHP so I'm having issues getting the images to show. This is for a client's website that turned out to be WAY more work than we anticipated. The code was originally pulling an image from a section they had titled 'Activities'. We moved away from that content and are using the 'Posts' section again for this.
The code for that area is below. Any help is GREATLY appreciated. Thanks
<?php
$args = array('post_type' => 'post','order'=> 'ASC', 'posts_per_page' =>2,'orderby' => 'date','order'=>'DESC');
// The Query
query_posts( $args );?>
<ul>
<?php while ( have_posts() ) : the_post(); ?>
<?php //$post_thumbnail_id = get_post_thumbnail_id( get_the_ID () ); ?>
<?php /*$image_id = get_post_thumbnail_id(get_the_ID ());
$image_url = wp_get_attachment_image_src($image_id,'post-thumb', true);*/
?>
<li>
<?php //if ( has_post_thumbnail() ){ ?>
<?php if(get_field('show_this_activity_image_in_home_page')){?>
<div class="actImg">
<?php
$attachment_id = get_field('show_this_activity_image_in_home_page');
$size = "post-thumb"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
OK, the code looks like it is using Advanced Custom Fields which would have been returning the ID of a media item. As you have moved from a Custom Post Type to the native Posts these ACF values will no longer be available.
As you have setup the post data with the_post(); you should be able to get the thumbnail url with get_the_post_thumbnail_url();
See https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/ for an explanation of this function.
In my wordpress theme I use function to display the first image of the post in my index.php
for me, I don't want to add support thumbnails in my theme. its so annoying!
I watched an tutorial that teach how to display images in the left beside the the excerpt of the post by adding a padding-left for the excerpt and floating the image to the left so that the image is in the left and the excerpt is beside the image. Also, the tutorial teach you how to do nothing when there's no image for the post (to do nothing=to not push the excerpt and to not add floating to the image so the format of the post look normal).
He did this by using the fucntion if has_post_thumbnail = do something (add a padding-left to the excerpt and float image to the right) and if the post doesn't have thumbnail = do nothing. that's it.
Here's the code:
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<article class="post <?php if ( has_post_thumbnail() ) { ?>has-thumbnail <?php } ?>">
<!-- post-thumbnail -->
<div class="post-thumbnail">
<?php the_post_thumbnail('small-thumbnail'); ?>
</div><!-- /post-thumbnail -->
<h2><?php the_title(); ?></h2>
<p class="post-info"><?php the_time('F j, Y g:i a'); ?> | by <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>">
Now I want to do the same but I don't want add support for thumbnails in my theme and i'm not planning to support thumbnails.
I tried to replace has_post_thumbnail() with the function I use get post first image but it didn't work.
I really need to do this in my website. So, is there's any solution guys?
You should really consider using featured images. It only takes adding a single line to functions.php to enable them...
However, if you still don't want to, you can check for image attachments to posts using the following:
// Get images attached to the current post
$images = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image') );
// If there are images
if ( $images ) {
// Show stuff
} else {
// Do nothing
}
the_post_thumbnail calls the featured image... which is what you supposedly don't want to do.
Instead, you need to check the post contents for an image. There's answers here: https://wordpress.stackexchange.com/questions/60245/get-first-image-in-a-post
Use has_post_thumbnail() conditional function. See docs.
But if you really don't want to use post thumbnails (you should, but you're the boss), see this question.
I'm actually working on a wordpress website with a Dessign.net theme (the pixel one) which got a beautiful full-page slider on the front page.
The slider shows the featured image of selected posts (post for which i've checked "show in slideshow" in the meta box field on edit page).
Those featured image are used in the same way for different view on the site (eg. thumbnails). I need them for the thumbnails, but i'ld like another image (still relative to selected posts) for the home-page slider.
I've found that "Dynamic Featured Image" plugins for wordpress but now i can't achieve to get the second featured image url in the slider's loop.
Here's the part of code for the slider, as it was with the theme:
<ul>
<?php
$slider_arr = array();
$x = 0;
$args = array(
//'category_name' => 'blog',
'post_type' => 'post',
'meta_key' => 'ex_show_in_slideshow',
'meta_value' => 'Yes',
'posts_per_page' => 99
);
query_posts($args);
while (have_posts()) : the_post();
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'full' );
//$thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'large' );
$img_url = $thumb['0'];
?>
<li data-background="<?php echo $img_url; ?>" onclick="location.href='<?php the_permalink(); ?>';" style="cursor:pointer;">
</li>
<?php array_push($slider_arr,get_the_ID()); ?>
<?php $x++; ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
Now i've tried to put the code found on the plugin github page:
if( class_exists('Dynamic_Featured_Image') ) {
global $dynamic_featured_image;
$thumb = $dynamic_featured_image->get_featured_images( );
//You can now loop through the image to display them as required
}
in place of $thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'full' );
But $thumb return array as a string
I tried a few different things but i'm not fluent in php.
Hope this is understandable.
I had to recently look for answers on something like this myself. The plugin author is great at explaining how to actually set up the plugin, but doesn't really say how to get the images, leaving it up to the developer. So, I feel you.
If I understand you correctly, you need to get the featured image from the plugin, not the featured image included in WordPress.
<?php global $dynamic_featured_image;
$featured_images = $dynamic_featured_image->get_featured_images( get_the_ID() );
//You can now loop through the image to display them as required
foreach($featured_images as $featured_image) {
echo "<a href='".get_the_permalink()."' class='slide'>";
echo "<span> <img src='".$featured_image['full']."' /> </span>";
echo "</a>";
} ?>
In this plugin, you can create infinite amounts of featured images per post/page. This code above is only for grabbing the first image created by the plugin. It's $featured_image['full'] that calls the image itself.
You can change the type of image shown to other sizes as well, including any custom sizes you create. The code to use those sizes can be found on this post.
A site I made required the display of WordPress post information (title, tags, thumbnails). The PHP code which gets this information from published posts works on my localhost and the post data displays properly.
I just uploaded the site onto my host's server, configured the WP settings to be identical to my localhost WP install, and while 99% of the site works as expected, the information for posts does not display.
There are 3 test posts on the live site currently. I can see 3 article tags through the DOM inspector, so the WP loop to retrieve posts works fine. However, within each article tag, the entry-content tag, which should contain the data, is empty.
All other php code is executing but this little block. Anyone have some advice on resolving the issue?
Here are images (yellow highlight showing the article tags):
First one of the live server, the empty div:
http://imgur.com/rF229Wk
Second on of the localhost, data displaying fine:
http://imgur.com/tMCOg0S
Here is the relevant code:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry-content">
<?php
$images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
if ( $images ) :
$total_images = count( $images );
$image = array_shift( $images );
$image_img_tag = wp_get_attachment_image( $image->ID, 'thumbnail' );
?>
and just below that code is the PHP that displays the thumbnail...(the PHP display code for the other post data like title is omitted). None of the data displays on the site:
<figure class="gallery-thumb">
<span class="image-wrapper">
<?php echo $image_img_tag; ?>
All these divs and the figure tags are closed properly further below the page.
Thanks for any help.
Looks like the $images variable is empty (or false) and your if statement doesn't end before <figure>.
Images are attached to a post on upload only. If you use an image from the media center for featured image it won't be attached to the post. You can retrieve this image by getting the meta field using get_post_meta( $post->ID, 'featured_image' ) or if you are using the featured image you can also use get_the_post_thumbnail( $post->ID ).