Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

2 Answers 2

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. –  m0r7if3r Mar 6 '12 at 12:01

Your Answer

 
discard

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.