我正在创建一些 WordPress 短代码,旨在在页面上提供内部导航(一个页面有很多内容部分和它自己的菜单)。
这是我的:
//menu
function internal_menu($atts) {
extract(shortcode_atts(array(
'href1' => '#jl1',
'href2' => '#jl2',
'href3' => '#jl3',
'href4' => '#jl4',
), $atts));
return '<div id="internalPageMenu">
<ul>
<li><a href="' . $href1 . '"><i class="fa fa-bars"></i>link 1</a></li>
<li><a href="' . $href2 . '">link 2</a></li>
<li><a href="' . $href3 . '">link 3</a></li>
<li><a href="' . $href4 . '">link 4</a></li>
</ul>
</div>';
}
add_shortcode('internal-menu', 'internal_menu');
//menu target
function internal_menu_target($atts) {
extract(shortcode_atts(array(
'id' => 'jl1',
'text' => '',
), $atts));
return '<h3 id="' . $id . '">' . $text . '</h3>';
}
add_shortcode('internal-menu-target', 'internal_menu_target');
并在我的 Wordpress 管理面板中使用它:
[internal-menu]
[internal-menu-target id="jl1"]
Some content
[internal-menu-target id="jl2"]
...etc...
如何使菜单动态化(不限于它可以包含的项目数)?例如,短代码为:
[internal-menu targets="jl1, jl2, jl3, jl4, jl5, ...etc..."]
最佳答案
foreach 将是您的答案。在我看来,这将是最简单和最干净的。在我给你一个代码示例之前,让我们分析你的代码并看看你的所有缺陷以及我们将如何纠正它们
永远不要使用extract()。 exctract() 即时创建变量,这是有问题的。您无法正确调试 extract()(如果您能的话),所以当它失败时,您的工作就真的没有必要了。由于这些原因,它已从核心和法典中完全删除。参见 trac ticket 22400 .你应该有一个邪恶列表,其中 query_posts 和 extract() 位于前两个位置,我知道这两个有多糟糕。
您没有清理和验证输入数据,这可能导致黑客将 jquery 注入(inject)您的代码以入侵您的网站。 永远不要相信任何来自用户端和 URL 的数据,它可能被感染。
如您所知,从您的代码中可以看出,短代码不能除数组值外,值必须是字符串。在您的情况下,我们需要从字符串值创建一个数组。同样,因为您不能相信用户不在逗号前后使用空格,所以明智的做法是删除所有空格(如果有的话),以便您的 explode 函数正确创建您的数组
使用这种新方法,您需要确保字符串中的值顺序正确,并且字符串的长度正确。如果没有,你会得到意想不到的输出
让我们来处理第一个短代码:(请注意:下面的所有代码都未经测试。它可能有错误或有语法错误)
内部菜单//menu
function internal_menu( $atts )
{
$attributes = shortcode_atts(
array(
'href' => '',
),
$atts
);
$output = '',
// Check if href has a value before we continue to eliminate bugs
if ( !$attribute['href'] )
return $output;
// Create our array of values
// First, sanitize the data and remove white spaces
$no_whitespaces = preg_replace( '/\s*,\s*/', ',', filter_var( $attributes['href'], FILTER_SANITIZE_STRING ) );
$href_array = explode( ',', $no_whitespaces );
$output .= '<div id="internalPageMenu">';
$output .= '<ul>';
foreach ( $href_array as $k => $v ) {
// From your code, link 1 is different, so I kept it as is
if ( $k == 0 ) {
$output .= '<li><a href="#' . $v . '"><i class="fa fa-bars"></i>link 1</a></li>';
} else {
$output .= '<li><a href="#' . $v . '">link ' . ($k + 1 ) . '</a></li>';
}
}
$output .= '</ul>';
$output .= '</div>';
return $output;
}
add_shortcode( 'internal-menu', 'internal_menu' );
然后您可以使用下面的简码
[internal-menu href='jl1, jl2, jl3, jl4']
内部菜单目标//menu target
function internal_menu_target($atts)
{
$attributes = shortcode_atts(
array(
'id' => '',
'text' => '',
),
$atts
);
$output = '',
// Check if href has a value before we continue to eliminate bugs
if ( !$attribute['id'] || !$attribute['text'] )
return $output;
// Create our array of values
// First, sanitize the data and remove white spaces
$no_whitespaces_ids = preg_replace( '/\s*,\s*/', ',', filter_var( $attributes['id'], FILTER_SANITIZE_STRING ) );
$ids_array = explode( ',', $no_whitespaces_ids );
$no_whitespaces_text = preg_replace( '/\s*,\s*/', ',', filter_var( $attributes['text'], FILTER_SANITIZE_STRING ) );
$text_array = explode( ',', $no_whitespaces_text );
// We need to make sure that our two arrays are exactly the same lenght before we continue
if ( count( $ids_array ) != count( $text_array ) )
return $output;
// We now need to combine the two arrays, ids will be keys and text will be value in our new arrays
$combined_array = array_combine( $ids_array, $text_array );
foreach ( $combined_array as $k => $v )
$output .= '<h3 id="' . $k . '">' . $v . '</h3>';
return $output;
}
add_shortcode('internal-menu-target', 'internal_menu_target');
您可以按如下方式使用此简码:
[internal-menu-target id='1,2,3,4' text='text 1, text 2, text 3, text 4']
关于php - Wordpress 短代码传递值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31307306/
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
我正在尝试在Ruby中制作一个cli应用程序,它接受一个给定的数组,然后将其显示为一个列表,我可以使用箭头键浏览它。我觉得我已经在Ruby中看到一个库已经这样做了,但我记不起它的名字了。我正在尝试对soundcloud2000中的代码进行逆向工程做类似的事情,但他的代码与SoundcloudAPI的使用紧密耦合。我知道cursesgem,我正在考虑更抽象的东西。广告有没有人见过可以做到这一点的库或一些概念证明的Ruby代码可以做到这一点? 最佳答案 我不知道这是否是您正在寻找的,但也许您可以使用我的想法。由于我没有关于您要完成的工作
我的主要目标是能够完全理解我正在使用的库/gem。我尝试在Github上从头到尾阅读源代码,但这真的很难。我认为更有趣、更温和的踏脚石就是在使用时阅读每个库/gem方法的源代码。例如,我想知道RubyonRails中的redirect_to方法是如何工作的:如何查找redirect_to方法的源代码?我知道在pry中我可以执行类似show-methodmethod的操作,但我如何才能对Rails框架中的方法执行此操作?您对我如何更好地理解Gem及其API有什么建议吗?仅仅阅读源代码似乎真的很难,尤其是对于框架。谢谢! 最佳答案 Ru