Is there a simpler way to write this?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Is there a simpler way to write this?

Post 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]; ?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Is there a simpler way to write this?

Post 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 )));
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: Is there a simpler way to write this?

Post 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!
Post Reply