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];
Related
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.
I seem to be struggling with something in woocommerce.
I have created a single product page template which currently shows the product's featured image at the top of the page in the banner.
However, instead of the product featured image, I would like the banner to display the product category image of the category the product belongs to.
They will only ever belong to one category, so having a fallback isn't that important (I think?!)
To get the product thumbnail I'm using this:
<?php
$image = get_field('product_single_image', 'options');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>">
<?php endif; ?>
So it looks like I could use the same code but replace the product_single_image with get_category_thumbnail or something...
But I think I need to find the category first? I'm not sure where to go from here.
Any help would be massively appreciated.
The best way to get current category ID on single product page and image
global $wp_query;
$terms_post = get_the_terms( $post->cat_ID , 'product_cat' );
foreach ($terms_post as $term_cat) {
$term_cat_id = $term_cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $term_cat_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id );
echo '<img src="' . $image_url . '">';
}
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.
So here is what i'm trying to do. I have created a custom taxonomy called "country" and within that custom taxonomy I have categories of different countries like, USA, France, Germany, England and so on. I created a ACF(Advance custom field) Field Group that lets me add a image to each country, I have this called "flag". So when I make a post and I check the country under the custom taxonomy I need to pull that image (flag) associated with that category and display it on my page.
I have seen lots of post on what im looking to do but I cant seem to get any to work. Here is the code im TRYING to use.
<?php
$attachment_id = get_field( 'flag', 'country_' . $queried_object->term_id );
$thumb = wp_get_attachment_image_src( $attachment_id );
var_dump($attachment_id);
?>
<p><img src="<?php echo $thumb[0]; ?>" /></p>
Its bring back a "NULL" and not my image id.
Any ideas on what i'm doing wrong or any advice you and lend me?
have you defined $queried_object?
EDIT:
TRY THIS OUT
<?php
$term =$wp_query->queried_object;
$attachment_id = get_field( 'flag', 'country_' . $term->term_id );
$thumb = wp_get_attachment_image_src( $attachment_id );
var_dump($attachment_id);
?>
<p><img src="<?php echo $thumb[0]; ?>" /></p>
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;?>