[英]Redux Options Framework - Slides search
Im currently using Redux plugin for WordPress with the slides "field": https://docsv3.redux.io/core/fields/slides/index.html .我目前使用带有幻灯片“字段”的 WordPress 的 Redux 插件: https ://docsv3.redux.io/core/fields/slides/index.html。
The problem is i have 55+ slides.问题是我有 55 多张幻灯片。
I can get slide with index "0" this way:我可以通过这种方式获得索引为“0”的幻灯片:
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.但是我需要快速获取具有特定标题的幻灯片,例如“主页”,我目前这样做的方式是使用 PHP array_search() 并在找到之前浏览 $redux_demo['opt-slides']中的每张幻灯片一个具有给定名称的。
Isn't there a more optimized (performance wise) solution to this problem?, similar to:这个问题没有更优化(性能方面)的解决方案吗?类似于:
$redux_demo['opt-slides']["homepage"]['url']
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.您可以尝试下一个代码,一个需要 2 个参数的小函数,您的幻灯片数组标题中的搜索词(确切的标题,到目前为止,但您可以使用不幼稚的东西(例如正则表达式)来更改此行为。最后返回幻灯片的索引.
$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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.