The images that I'm querying from my wordpress media gallery are appearing correctly. But, I also want them to link to my custom attachment page and I can't get it to work
<?php
$query_images_args = array(
'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1,
);
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= wp_get_attachment_url( $image->ID );
}
foreach ($images as $img) {
$url = get_attachment_link($img->ID);
echo '' . '<img src="' . $img . '" alt="" />' . '';
}
?>
Skip the second foreach loop. You have everything you need in the first one:
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= wp_get_attachment_url( $image->ID );
$url = get_attachment_link($image->ID);
echo '' . '<img src="' . wp_get_attachment_url( $image->ID ) . '" alt="" />' . '';
}
wp_get_attachment_url() returns the URI of the image, see http://codex.wordpress.org/Function_Reference/wp_get_attachment_url; you were trying to get the ID from this non-object.
Related
I am trying to create a a page in wordpress where I show 5 recent posts.
I was able to successfully do that.
However, I am not able to retrieve images that are present inside that post.
I am using this code to retrieve post url and post image. (Post URL works fine!)
$args = array('numberposts'=>'5');
$recentposts = wp_get_recent_posts($args);
foreach($recentposts as $post){
$v = $post['ID'];
$postlink = get_permalink($v);
$postimg = get_the_post_thumbnail($v);
}
try below code
$posts = get_posts( array( 'posts_per_page' => 5 ) );
foreach ( $posts as $_post ) {
if ( has_post_thumbnail( $_post->ID ) ) {
echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . esc_attr( $_post->post_title ) . '">';
echo get_the_post_thumbnail( $_post->ID, 'thumbnail' );
echo '</a>';
}
}
I have this code where I'm trying to display all three images attached to visual composer's custom "attach_images" element type that has param "macimgs". The problem is that only the last one image will display and if I inspect the container that holds images, I see only one image inside instead of three.
Any ideas of what to modify here?
$gallery = shortcode_atts(
array(
'macimgs' => 'macimgs',
), $atts );
$image_ids=explode(',',$gallery['macimgs']);
$image_no = 1;
foreach( $image_ids as $image_id ){
$images = wp_get_attachment_image_src( $image_id, 'full' );
$output ='
<img src="'. $images[0] .'" alt="" />
';
$image_no++;
}
return $output;
}
i think you forget to concatenate the $output variable.
try this code
$gallery = shortcode_atts(
array(
'macimgs' => 'macimgs',
), $atts );
$image_ids = explode(',',$gallery['macimgs']);
$output = '';
$image_no = 1;
foreach( $image_ids as $image_id ){
$images = wp_get_attachment_image_src( $image_id, 'full' );
$output .='<img src="'. $images[0] .'" alt="" />';
$image_no++;
}
return $output;
I am using this code here to get images from media library using custom taxonomy. It gets the image url fine, but I want to get image id and title. How do I do that?
This is what I have so far.
function get_images_from_media_library($cat) {
$args = array(
'post_type' => 'attachment',
'post_mime_type' =>'image',
'post_status' => 'inherit',
'posts_per_page' => 6,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'gallery-category',
'field' => 'slug',
'terms' => $cat
)
)
);
$query_images = new WP_Query( $args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= $image->guid;
echo $image->ID; // Returns image ID, but I need it in display_images_from_media_library function
}
return $images;
}
function display_images_from_media_library($cat) {
$imgs = get_images_from_media_library($cat);
foreach($imgs as $img) {
$html .= '<img src="' . $img . '" alt="">';
}
return $html;
}
get_images_from_media_library() returns an array of image URLs therefore display_images_from_media_library() never has access to anything other than those URLs. If you wish to access the ID and title as well you'll need to make a couple of updates.
Inside get_images_from_media_library(), change this:
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= $image->guid;
echo $image->ID; // Returns image ID, but I need it in display_images_from_media_library function
}
return $images;
To this:
return $query_images->posts;
The original code looped through the images and added their URLs to a new array. Here we're going to return the images in their original form.
Then we need to update the function that uses those images.
Change this:
$html .= '<img src="' . $img . '" alt="">';
To this:
$html .= '<img src="' . $img->guid . '" alt="">';
In my answer I've modified the code you've written to arrive at the desired result. I would however advise that you change the way you're retrieving the images in the first place. WP_Query isn't the most efficient way of handling it. Consider get_posts() instead.
I get the first image from post whith this code:
function first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
$images = array(
'white5px.jpg',
);
$image = $images[array_rand($images)];
$first_img = "/wp-content/themes/tabs/images/" . $image . "";
}
return $first_img;}
?>
<img src="<?php echo first_image() ?>"title="<?php the_title(); ?>" alt="<?php the_title(); ?>"/>
Everything works fine except when I use the galery - the image not displayed.
Live example:http://beardhouse.com.ua/?cat=2 Why it doesn't work and how I can solve this problem?
Everything works fine except when I use the galery - the image not
displayed.
Galleries are not stored in the post body as full img tags, which is what your regex looks for. A gallery is saved to the post body as a shortcode which is processed on display and transformed into the gallery you see.
That is, if you echo $post->post_content what you will see is something like this:
[gallery ids="729,732,731,720"]
The quick and dirty solution would be to process that shortcode before your regex gets involved.
$content = do_shortcodes($post->post_content);
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
$first_img = $matches [1] [0];
I can't help but think that that is a somewhat clumsy and inefficient solution. Most images in WordPress are "attachments" to the post, so you might be better off querying for attachments directly, as per this example from the Codex:
function echo_first_image( $postID ) {
$args = array(
'numberposts' => 1,
'order' => 'ASC',
'post_mime_type' => 'image',
'post_parent' => $postID,
'post_status' => null,
'post_type' => 'attachment',
);
$attachments = get_children( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) ? wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment->ID, 'full' );
echo '<img src="' . wp_get_attachment_thumb_url( $attachment->ID ) . '" class="current">';
}
}
}
To get First Image of Post Enter this code in Function.php file
function catch_that_image(){
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); $first_img = $matches [1] [0];if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";}return $first_img;}
}
add this code in post loop
I have a gallery which should display 3 columns, the first two columns have a certain class and work absolutely fine but I am trying to add a "last class" to my third column - for some reason it's not working. Find my code below:
<?php
$mod =1;
if ( $images = get_posts(array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',)))
{
foreach( $images as $image ) {
$attachmenturl=wp_get_attachment_url($image->ID);
$attachmentimage=wp_get_attachment_image_src( $image->ID, full );
$imageDescription = apply_filters( 'the_description' , $image->post_content );
$imageTitle = apply_filters( 'the_title' , $image->post_title );
if ($mod % 3 == 0) {
$class = "gallery-entry-img-last";
}else{
$class = "gallery-entry-img";
}
echo '<div class="'.$class.'"><div class="gallery-entry-img-l"><a rel="next" href="' . $attachmentimage[0] . '"><span class="rollover" ></span><img src="library/tools/timthumb.php?src=' . $attachmentimage[0] . '&w=270&h=198" alt="" /></div></div>';
}
$mod++;
}
else {
echo "No Image";
}
?>
Some expert advise would be greatly appreciated.
increment your $mod variable inside foreach loop, like:
foreach(.....) {
......
$mod++;
}