WP dynamic featured image - Can't get second featured image url - php

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.

Related

Wordpress Show Featured Image on Main Page for Blog Snippet/Link

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.

Show post excerpts with title and image attachment thumbnail

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!

Wordpress if post has an image I want to display that image if doesn't (do nothing)

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.

Get URL To Full Size Image And Add It To Specific Word In A Post

I want to hyperlink a specific word in the post to the full image which is stored on wp-content/uploads.
I add a line of words "View Full Size" on single.php so it is displayed on the bottom of each created post and I want to hyperlink those words to the full size image attached on that post.
How to do it with PHP?
I've used this snippet a couple times, It grabs the featured image, I've added the else option so you can set a default image if needed. I hope this is what you where looking for. Any problems I'll be glad to help.
<?php if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); $alt = get_post_meta(get_post_thumbnail_id( $post->ID ), '_wp_attachment_image_alt', true);?>
View Full Size
<?php else: ?>
<?php endif;?>

WP Featured Image URI instead of Custom Field

I am in a situation with a theme where I can't rely on custom-fields in wordpress and need to use the "featured image" feature instead.
This is the code that calls the custom field
<?php $thumbnail_image = get_post_meta($post->ID, 'post-image', true); ?>
I can't find any documentation on "featured image" in the Wordpress codex, but is it possible to call the url of the featured image of a page in this example?
This should get you going
http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
Edit:
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id() );
$image_src = $image_attributes[0];

Categories