Redux Options Framework - Slides search

143 views Asked by At

Im currently using Redux plugin for WordPress with the slides "field": https://docsv3.redux.io/core/fields/slides/index.html .

The problem is i have 55+ slides.

I can get slide with index "0" this way:

if (isset($redux_demo['opt-slides']) && !empty($redux_demo['opt-slides'])) {
    echo 'Slide 1 Title: '         . $redux_demo['opt-slides'][0]['title'];
    echo 'Slide 1 URL: '           . $redux_demo['opt-slides'][0]['url'];
}

However i need to quickly get the slide with a specific title, like "homepage", the way im currently doing this is using PHP array_search() and go through every single slide in $redux_demo['opt-slides'] before it finds the one with the given name.

Isn't there a more optimized (performance wise) solution to this problem?, similar to:

$redux_demo['opt-slides']["homepage"]['url']
1

There are 1 answers

2
Manuel Glez On

You could try the next code, a little function that needs 2 params your slides array The search term in Title (exact title, by now but you could change this behaviour with something not naive like a regex expression. Finally return the index of your slide.

$redux_demo =
    [
        0 => [
            'title' => 'Mi title',
            'url' => 'https//....'
        ],
        1 => [
            'title' => 'Mi title2',
            'url' => 'https//....'
        ]

    ];

//So you can 

function search_redux($redux_demo,$search){
  if (isset($redux_demo) && !empty($redux_demo)) {

    foreach($redux_demo as $index=>$slide){
      
      if($slide['title']===$search){
        return $index;
      } 
    }
 
  }
   
   

};

$result=search_redux($redux_demo,'Mi title2');