Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

84
Views
If statement within return

How do I add an if statement in a return statement of this sort:

return {
    ID: select('core/editor').getEditedPostAttribute('featured_media'),
    if (ID) {
        media: select('core').getMedia( ID )
    }
};

Clearly, I can't just drop an if statement if () directly within return {}.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There are 2 things you can do, either use a ternary operator inline:

return {
    ID: select('core/editor').getEditedPostAttribute('featured_media'),
    media: (ID ? select('core').getMedia( ID ) : undefined)
};

or create the object and conditionally add a new property

const obj = {
    ID: select('core/editor').getEditedPostAttribute('featured_media')
};

if(ID){
    obj.media = select('core').getMedia( ID )
}

return obj
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!