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?


2 Answers 2


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.

  • 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
    Commented Mar 6, 2012 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
    Commented Mar 6, 2012 at 14:31
  • @alex Cool. Mind sharing your solution by extending your question?
    – fuxia
    Commented Mar 6, 2012 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
  • tx i used your suggestion with a !is_array( $excerpts ) for caption with only one value
    – alex
    Commented Mar 6, 2012 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
    Commented Mar 6, 2012 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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