WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom function which looks if an attachement has a certain caption value, if so the attachement is selected, like so

 foreach ($attachments as $attachment) {
            if ($attachment->post_excerpt == 'single-gallery') {

But how can i give a attachement two or more values and select one? So if i give an attachement the caption values of

single-gallery | downloads

it will be selected for single gallery and downloads

Any suggestions?

share|improve this question
up vote 1 down vote accepted

You should not misuse the caption for such values. Just add separate fields for those data.

There are also plugins for media tags which may fit better to your needs.

share|improve this answer
your are right but i needed a quick fix, done that. Now i can dive deeper into your code. Don't like to use a plugin, i want to add the code to functions.php. Looking for a way to add an extra multi select checkboxes field to the attachments (single-gallery, multi-gallery, downloads) and use the values in my theme – alex Mar 6 '12 at 11:59
tx for helping me get on the right track. In short i added a multi checkbox and put a conditional in my theme. – alex Mar 6 '12 at 14:31
@alex Cool. Mind sharing your solution by extending your question? – toscho Mar 6 '12 at 14:48

You can use php's explode() function as so:

foreach( $attachments as $attachment ) {
    $excerpts = explode( ' | ', $attachment->post_excerpt );
    if ( is_array( $excerpts ) && in_array( 'single-gallery', $excerpts ) ) {
        //your stuff
share|improve this answer
tx i used your suggestion with a !is_array( $excerpts ) for caption with only one value – alex Mar 6 '12 at 11:52
If it's a single value, it will output an array with just one value in it. The only time it won't be an array is if you use an empty string or something. – mor7ifer Mar 6 '12 at 12:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.