Wordpress: - Pull All attachment images including the Featured image - wordpress

I'm having real difficulty pulling all atttachments including the image... The thing is, I need to be able to control how the content is displayed...
e.g.
<a href="(Path-to-full-image)" rel="customrel" class="aclasstodeine">
<img src="thethumbnailsize(defined in functions.php)" alt=""
class="aclasstodefine" />
</a>
REALLY hope someone can help

To get all attachment images you'll need to do a query.
$args = array(
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_status' => 'inherit',
'numberposts' => -1
);
$images = get_posts($args);
This will load any image attached to the post into the $images variable then do a foreach loop on them to get your images:
<?php foreach ($images as $i) : ?>
<a href="<?php wp_get_attachment_image_src($i->ID, 'full'); ?>" rel="customrel" class="aclasstodeine">
<img src="<?php wp_get_attachment_image_src($i->ID, 'thumbnail'); ?>" alt="" class="aclasstodefine" />
</a>
<?php endforeach; ?>
You can read up on wp_get_attachment_image_src here: http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

More likely it would look like this:
$args = array(
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_status' => 'inherit',
'numberposts' => 1//-1
);
$images = get_posts($args);
foreach ($images as $i) :
$poza_thumb = wp_get_attachment_image_src($i->ID, 'thumbnail');
//$poza_fullsize = wp_get_attachment_image_src($i->ID, 'full');
//print_r($poza_thumb);//you will see that $poza_thumb is an array containing url, width, height
?>
<img src="<?php echo $poza_thumb[0];?>" width="<?php echo $poza_thumb[1];?>" height="<?php echo $poza_thumb[2];?>" rel="customrel" class="aclasstodeine"/>
<?php
endforeach;

Related

Get image from a wordpress post

i have the title of my last 4 posts but i need the image post too. I'm terrible programmer `
include('../blog/wp-load.php'); // Blog path
// Get the last 4 posts
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 4,
'category' => 0,
'orderby' => 'post_date',
'post_type' => 'post',
'post_status' => 'publish'
));
// Display them as list
echo '<ul>';
foreach($recent_posts as $post) {
echo '<li>', $post['post_title'], '</li>';
}
echo '</ul>'`
i'm trying to do something like this:
I think you're looking for get_the_post_thumbnail Here's the Codex.
For example:
get_the_post_thumbnail( $postID,'medium', array( 'class' => 'aligncenter' ));
You might try something like this for your final product (untested):
// Display them as list
$output = '<ul>';
foreach($recent_posts as $post) {
$link = get_permalink($post['ID']);
$image = get_the_post_thumbnail( $postID,'medium', array( 'class' => 'aligncenter' ));
$title = $post['post_title'];
$output .= '<li>
<a href="'.$link.'">'.
$image
.'<h2>'.$title.'</h2>
</a>
</li>';
}
$output .= '</ul>';
echo $output;
It's must be work!
<?php
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 4,
'category' => 0,
'orderby' => 'post_date',
'post_type' => 'post',
'post_status' => 'publish',
));
foreach($recent_posts as $single_post){
$get_post_images = get_attached_media( 'image', $single_post['ID'] );
$get_post_images = array_shift( $get_post_images );
$first_image_url = $get_post_images->guid;
?>
<?php echo $single_post['post_title'] ?><br />
<img src="<?php echo $first_image_url; ?>" /><br />
<?php
}
?>
If you have't the post thumbnail, but have images inside post, then you need this solution which use inside the loop:
$get_post_images = get_attached_media( 'image', $post->ID );
$get_post_images = array_shift( $get_post_images );
//Get url for image
$first_image_url = $get_post_images->guid;
// Show the image
echo '<img src="'. $first_image_url .'" />';
I got it!
<ul>
<?php
include('esenergy/wp-load.php'); // Blog path
function recentPosts() {
$rPosts = new WP_Query();
$rPosts->query('showposts=3');
while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
<li>
<?php the_post_thumbnail('recent-thumbnails'); ?>
<h2><?php the_title();?></h2>
</li>
<?php endwhile;
wp_reset_query();
}
?>
</ul>
<?php echo recentPosts(); ?>

Wrong image displayed, despite the correct link

Here is the very weird issue I'm dealing with at the moment: I am trying to implement a lightbox effect in my wordpress theme. Following this tutorial here (the CSS code used is this one: link). I tried it as it is, it works very well.
But I'm not looking for applying the effect to the featured image but to all images in a post. I have changed the code to match my need, it works if I have only one picture in the post but if I add another one (or more) that's when the problem starts:
when I click on a picture, no matter which one, it's only the first one that get displayed in the large version. But when I check the source code, the relevant picture is called for the large version of each picture.
Here's the bit of code I'am using for this lightbox effect:
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'post_mime_type'=> 'image',
'orderby' => 'ID',
'order' => 'ASC',
);
$attachments = get_posts( $args );
?>
<div class="nine columns">
<div class="about">
<h2><?php the_title(); ?></h2>
<?php foreach ( $attachments as $attachment ) :
$attachments = get_posts( $args );
$small_image = wp_get_attachment_image_src($attachment->ID, 'thumbnail');
$large_image = wp_get_attachment_image_src($attachment->ID, 'full');
?>
<img src="<?php echo $small_image[0]; >" alt="small">
<img src="<?php echo $large_image[0]; ?>" alt="large">
<?php endforeach ?>
</div>
</div>
Thanks in advance for looking into my request
With kind regards
Wanderer
Your code has three problems:
1. should not call $attachments = get_posts( $args ); in foreach
2. the href of the small image link is incorrect
3. the id of large image link is incorrect
Try this:
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'post_mime_type'=> 'image',
'orderby' => 'ID',
'order' => 'ASC',
);
$attachments = get_posts( $args );
?>
<div class="nine columns">
<div class="about">
<h2><?php the_title(); ?></h2>
<?php foreach ( $attachments as $attachment ) :
$small_image = wp_get_attachment_image_src($attachment->ID, 'thumbnail');
$large_image = wp_get_attachment_image_src($attachment->ID, 'full');
?>
<img src="<?php echo $small_image[0]; ?>" alt="small">
<img src="<?php echo $large_image[0]; ?>" alt="large">
<?php endforeach ?>
</div>
</div>

showing wordpress featured image in slider

I want show featured image in my slider. so what i've done in my function.php is like below:
<?php
function revconcept_get_images($post_id) {
global $post;
$thumbnail_ID = get_post_thumbnail_id();
$images = get_children( array('post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
if ($images) :
foreach ($images as $attachment_id => $image) :
$img_alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); //alt
if ($img_alt == '') : $img_alt = $image->post_title; endif;
$big_array = image_downsize( $image->ID, 'large' );
$img_url = $big_array[0];
echo '<li>';
echo '<img src="';
echo $img_url;
echo '" alt="';
echo $img_alt;
echo '" />';
echo '</li><!--end slide-->';
endforeach; endif; }
?>
<div class="flexslider"> <!-- function called in index.php -->
<ul class="slides">
<?php revconcept_get_images("$post->ID"); ?>
</ul>
</div><!--end flexslider-->
Now i have two problem. no.1 My featured image is not showing in the slider. It's showing a broken link. but it can echo the post_id. no.2 I want show only 3/4 images from the latest post of cat=5. can anyone help me to do this ? note that i am using flexslider for my slider.
Ok, this is what I've tested, you may want to refine the query a bit, but for me it worked. In functions.php put
function revconcept_get_images($post_id) {
$images = get_posts( array(
'post_type' => 'attachment',
'hide_empty' => true,
'order' => 'ASC',
'orderby' => 'menu_order ID'
));
foreach ($images as $image) {
$image_url = $image->guid;
$img_alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true); //alt
echo '<li><img src="'.$image_url.'" alt="'.$img_alt.'" /></li>';
}
}
And in index.php put (where you want)
<div class="flexslider"> <!-- function called in index.php -->
<ul class="slides">
<?php revconcept_get_images($post->ID); ?>
</ul>
</div><!--end flexslider-->
If you want to show all featured images from posts you have just put 'posts_per_page' => -1, in the get_posts() array.

Wordpress get_posts by category

I have the following bit of code:
$args = array(
'posts_per_page' => -1,
'category' => 7,
'orderby' => 'name',
'order' => 'ASC',
'post_type' => 'product'
);
$posts = get_posts($args);var_dump($posts);
This should return one post I know that is in the category, but it isn't. If I leave out the 'category'-argument, I get all the products, so I know this should normally work. If I change the category to 1 and take out my custom post type (product), I get my default posts.
I can't see what's wrong with this. Can anyone spot what the problem is?
If you'd like you could accomplish the same thing but with WP_Query. This gets the post type "Product" with "posts_per_page" for the amount of posts, and "product_cat" for the product category. Hope this helps!
EDIT: If you'd like to do it your way maybe instead of "category" try "product_cat"
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<h2>Shoes</h2>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->

wordpress query to get all images from a post type randomly

i have the following query to get all images from the "myportfoliotype" post type which works fine.
However i would like all images pulled in to be random when the page is loaded / refreshed.
I followed a few tutorials, and came up with the following code:
<?php
$query = new WP_Query( array( 'post_type' => 'myportfoliotype', 'posts_per_page' => -1, 'orderby' => 'rand' ) );
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$do_not_duplicate = $post->ID;
$thumb_id = get_post_thumbnail_id( $post_id );
$image_query = new WP_Query( array('post__not_in' => array (MultiPostThumbnails::has_post_thumbnail('myportfoliotype', 'logo'), $thumb_id ), 'orderby' => 'rand' , 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => -1, 'post_parent' => get_the_ID() ) );
while( $image_query->have_posts() ) {
$image_query->the_post();
$do_not_duplicate = $post->ID;
//echo wp_get_attachment_image( get_the_ID() );
$image = wp_get_attachment_image_src(get_the_ID(), 'large');?>
<li> <a class="fancybox" rel="gallery1" href="<?php echo $image[0]; ?>"> <img src="<?php echo get_template_directory_uri(); ?>/js/timthumb.php?src=<?php echo $image[0]; ?>&w=137&h=137&f=2" alt="<?php the_title(); ?>" class="grey"/></a>
<img src="<?php echo get_template_directory_uri(); ?>/js/timthumb.php?src=<?php echo $image[0]; ?>&w=200&h=200" alt="<?php the_title(); ?>" class="color"/>
</li>
<?php
}
}
}
?>
I'm not quite sure if this is right? As mentioned before it pull in the images but not randomly....
Any help or guidance would be greatly appreciated.
Cheers, Dan
You probably have a plugin that hooks on posts_orderby filter.
If you don't want to disable this plugin, you can add 'suppress_filters'=>true to your WP_Query params.
I have solved my problem, and though i would post the answer incase anyone else was having the same problem using the "post type order" plugin.
In the setting page for "post type order" there is an option called "auto sort" you need to uncheck this this, and when making a query you should add the following:
$args = array(
'post_type' => 'post-type-here',
'orderby' => 'menu_order',
'order' => 'ASC'
);
As simple as that!
Thanks for all the help guys!

Resources