Need help with a code

Discussion in 'WordPress' started by rober7, Sep 18, 2011.

  1. #1
    i found this nice snippet on the web but because i don`t know php i need some help.
    This code extract the attached picture from post and display it as a shortcode but i want to make it generate thumbs automaticaly on my first page.
    function cwc_postimage($atts, $content = null) {
    	extract(shortcode_atts(array(
    		"size" => 'thumbnail',
    		"float" => 'none'
    	), $atts));
    	$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . get_the_id() );
    	foreach( $images as $imageID => $imagePost )
    	$fullimage = wp_get_attachment_image($imageID, $size, false);
    	$imagedata = wp_get_attachment_image_src($imageID, $size, false);
    	$width = ($imagedata[1]+2);
    	$height = ($imagedata[2]+2);
    	return '<div class="postimage" style="width: '.$width.'px; height: '.$height.'px; float: '.$float.';">'.$fullimage.'</div>';
    }
    add_shortcode("postimage", "cwc_postimage");
    Code (markup):
    and "[postimage]" is the shortcode to display the image
    i use "thumb" as custom field for images.
    any ideas how can i do it?
     
    rober7, Sep 18, 2011 IP
  2. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #2
    Does [postimage] call anything when you use it?

    Add this somewhere into your themes functions.php file and it should turn on.
     
    adbox, Sep 19, 2011 IP
  3. rober7

    rober7 Well-Known Member

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    138
    #3
    i don`t want to use [postimage] (the shortcode), in my first page show the pics with "thumb" at custom field ... i need someting like
    above code = thumb
     
    rober7, Sep 19, 2011 IP
  4. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #4
    What do you mean by this: " i need someting like above code = thumb"

    Does the shortcode work when you use it in content. Why post the shortcode code if you are not looking to use the shortcode?
     
    adbox, Sep 19, 2011 IP
  5. rober7

    rober7 Well-Known Member

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    138
    #5
    this thing
     function cwc_postimage($atts, $content = null) {
    	extract(shortcode_atts(array(
    		"size" => 'thumbnail',
    		"float" => 'none'
    	), $atts));
    	$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . get_the_id() );
    	foreach( $images as $imageID => $imagePost )
    	$fullimage = wp_get_attachment_image($imageID, $size, false);
    	$imagedata = wp_get_attachment_image_src($imageID, $size, false);
    	$width = ($imagedata[1]+2);
    	$height = ($imagedata[2]+2);
    	return '<div class="postimage" style="width: '.$width.'px; height: '.$height.'px; float: '.$float.';">'.$fullimage.'</div>';
    }
    add_shortcode("postimage", "cwc_postimage");
    Code (markup):
    ============ (equal)
    thumb
    Code (markup):
    this code "unction cwc_postimage ....." extract latest attached image from the post ... and i want to use it to display posts thumbs on my first page. i don`t want to modify template and to put [postimage] in the index.php file.
    my thumbs have "thumb" on custom fields (i need to copy image url and paste it in the custom fileds -> below posts in wp dashbord) and i heeded help to modify that code and add "thumb" automaticaly on image. :D
     
    Last edited: Sep 19, 2011
    rober7, Sep 19, 2011 IP
  6. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #6
    adbox, Sep 19, 2011 IP
  7. rober7

    rober7 Well-Known Member

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    138
    #7
    very slow loading with that plugin .... i tryed it.
     
    rober7, Sep 19, 2011 IP
  8. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #8
    So it looks like "and i want to use it to display posts thumbs on my first page. i don`t want to modify template and to put [postimage] in the index.php file. " is exactly what you need to do though. Why don't you want to add this shortcode forcibly to the template?
     
    adbox, Sep 19, 2011 IP
  9. rober7

    rober7 Well-Known Member

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    138
    #9
    you are acting like the police :) ... too many questions
    ... if somebody else can help me i`ll be glad ...
     
    Last edited: Sep 19, 2011
    rober7, Sep 19, 2011 IP
  10. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #10
    Yes yes need to know reasons to give best solutions. I do not care what you are doing though, just wondering why it is bad to modify the template in your case, so I am surprised at your response.

    You could use an ad injection plugin like WP Traffic Tools or possible a free one like Ad Injection to auto-insert the shortcode somewhere in your post without modifying the theme code.
     
    adbox, Sep 20, 2011 IP
  11. rober7

    rober7 Well-Known Member

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    138
    #11
    ok .. i`m sorry for that ... but as i told before i`m a newbie .. i dont know php and css ... and when i tryed last time to change something didn`t work as planned ... and i saw that piece of code that i need to put in function.php ... thats easy ...
     
    rober7, Sep 21, 2011 IP
  12. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #12
    The ad injection might be your best bet then. This way you can paste the shortcode into the ad template and just insert it into your posts. Ad Injection plugin is free.
     
    adbox, Sep 21, 2011 IP
  13. rober7

    rober7 Well-Known Member

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    138
    #13
    ok ... but will appear 2 thumb ... one fro theme and 1 from shartcode ... do you know how to make what i want or you need many posts?
     
    rober7, Sep 21, 2011 IP
  14. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #14
    adbox, Sep 21, 2011 IP
  15. radiusdanu

    radiusdanu Notable Member

    Messages:
    417
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    245
    #15
    radiusdanu, Sep 21, 2011 IP
  16. rober7

    rober7 Well-Known Member

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    138
    #16
    not working ... do you know how to make with the code above ... or not?
     
    rober7, Sep 21, 2011 IP