Page 1 of 1
Is there a simpler way to write this?
Posted: Tue Feb 26, 2013 6:43 pm
by someguyhere
I'm pretty sure this can be written more efficiently, but can't get it to work without declaring the variable and then echoing the resulting array as two separate lines of code. Any ideas?
Code: Select all
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID )); echo $image[0]; ?>
Re: Is there a simpler way to write this?
Posted: Tue Feb 26, 2013 6:53 pm
by requinix
PHP 5.4 supports the syntax you'd expect. Until then you can use current().
Code: Select all
$image = current( wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID )));
Re: Is there a simpler way to write this?
Posted: Wed Feb 27, 2013 8:27 am
by someguyhere
Works like a charm! Thanks!
I modified it slightly from what you showed me due to how it's used, but this is what I went with:
Code: Select all
current( wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID )))
(it's part of a social media sharing button, so I just needed to pull the URL to dynamically plug the image URL into the button code.
Thanks again!