Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-21-2012, 03:50 PM   PM User | #1
spudnic072
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
spudnic072 is an unknown quantity at this point
Question Wordpress and clickable image.

I am using this code here to get images that have been attached to my posts and to display those attachments in their respective posts on my wordpress blog.

PHP Code:
<?php //Get image genererated from form
$argsThumb = array(
    
'order'          => 'ASC',
    
'post_type'      => 'attachment',
    
'post_parent'    => $post->ID,
    
'post_mime_type' => 'image',
    
'post_status'    => null
);
$attachments get_posts($argsThumb);
if (
$attachments) {
    foreach (
$attachments as $attachment) {
        
//echo apply_filters('the_title', $attachment->post_title);
        
echo '<img src="'.wp_get_attachment_url ($attachment->ID'thumbnail'falsefalse).'" />';
    }
}
?>
This works great. But I want to be able to click on the image and have the images full size image open, because im am limiting the size that the image is displayed in the post.

To accomplish this I modified the above code to make the image clickable. That also works great. But it is making the entire content of the post clickable. IE: You can click on both the image and the text content in the post. I only want to be able to click on the image.

PHP Code:
<?php //Get image genererated from form
$argsThumb = array(
    
'order'          => 'ASC',
    
'post_type'      => 'attachment',
    
'post_parent'    => $post->ID,
    
'post_mime_type' => 'image',
    
'post_status'    => null
);
$attachments get_posts($argsThumb);
if (
$attachments) {
    foreach (
$attachments as $attachment) {
        
//echo apply_filters('the_title', $attachment->post_title);
        
echo '<a href='.wp_get_attachment_url($attachment->ID'thumbnail'falsefalse).'><img src="'.wp_get_attachment_url ($attachment->ID'thumbnail'falsefalse).'" />';
    }
}
?>
If anyone can show me how to edit this code so that only the image the code is calling will be clickable thatd be great. Thanks.
spudnic072 is offline   Reply With Quote
Old 09-21-2012, 04:04 PM   PM User | #2
Rowsdower!
Senior Coder

 
Rowsdower!'s Avatar
 
Join Date: Oct 2008
Location: Some say it's everything.
Posts: 2,007
Thanks: 5
Thanked 395 Times in 388 Posts
Rowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura aboutRowsdower! has a spectacular aura about
Quote:
Originally Posted by spudnic072 View Post
I am using this code here to get images that have been attached to my posts and to display those attachments in their respective posts on my wordpress blog.

PHP Code:
<?php //Get image genererated from form
$argsThumb = array(
    
'order'          => 'ASC',
    
'post_type'      => 'attachment',
    
'post_parent'    => $post->ID,
    
'post_mime_type' => 'image',
    
'post_status'    => null
);
$attachments get_posts($argsThumb);
if (
$attachments) {
    foreach (
$attachments as $attachment) {
        
//echo apply_filters('the_title', $attachment->post_title);
        
echo '<img src="'.wp_get_attachment_url ($attachment->ID'thumbnail'falsefalse).'" />';
    }
}
?>
This works great. But I want to be able to click on the image and have the images full size image open, because im am limiting the size that the image is displayed in the post.

To accomplish this I modified the above code to make the image clickable. That also works great. But it is making the entire content of the post clickable. IE: You can click on both the image and the text content in the post. I only want to be able to click on the image.

PHP Code:
<?php //Get image genererated from form
$argsThumb = array(
    
'order'          => 'ASC',
    
'post_type'      => 'attachment',
    
'post_parent'    => $post->ID,
    
'post_mime_type' => 'image',
    
'post_status'    => null
);
$attachments get_posts($argsThumb);
if (
$attachments) {
    foreach (
$attachments as $attachment) {
        
//echo apply_filters('the_title', $attachment->post_title);
        
echo '<a href='.wp_get_attachment_url($attachment->ID'thumbnail'falsefalse).'><img src="'.wp_get_attachment_url ($attachment->ID'thumbnail'falsefalse).'" />';
    }
}
?>
If anyone can show me how to edit this code so that only the image the code is calling will be clickable thatd be great. Thanks.
You need to close your anchor tag, like so:

PHP Code:
<?php //Get image genererated from form
$argsThumb = array(
    
'order'          => 'ASC',
    
'post_type'      => 'attachment',
    
'post_parent'    => $post->ID,
    
'post_mime_type' => 'image',
    
'post_status'    => null
);
$attachments get_posts($argsThumb);
if (
$attachments) {
    foreach (
$attachments as $attachment) {
        
//echo apply_filters('the_title', $attachment->post_title);
        
echo '<a href='.wp_get_attachment_url($attachment->ID'thumbnail'falsefalse).'><img src="'.wp_get_attachment_url ($attachment->ID'thumbnail'falsefalse).'" /></a>';
    }
}
?>
I think that should sort it out for you.

On a related note, HTML validation of the generated page would have pointed this out to you as well, so remember your old pal at http://validator.w3.org/ when things don't work as expected.
__________________
The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
See Mediocrity in its Infancy
It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
Seek and you shall find... basically:
validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting
Rowsdower! is offline   Reply With Quote
Old 09-21-2012, 04:39 PM   PM User | #3
spudnic072
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
spudnic072 is an unknown quantity at this point
You da man.

Ill give it a shot
spudnic072 is offline   Reply With Quote
Old 09-21-2012, 05:13 PM   PM User | #4
spudnic072
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
spudnic072 is an unknown quantity at this point
worked great.

thanks again
spudnic072 is offline   Reply With Quote
Reply

Bookmarks

Tags
clickable, href, image, php, wordpress

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:05 PM.


Advertisement
Log in to turn off these ads.