【发布时间】:2012-01-31 11:38:48
【问题描述】:
我整天都在尝试做这个循环,但我想不通。
我有一个名为 e-commerce 的 wordpress 插件。在单个产品页面上,我需要创建一个简单的 JavaScript 开关 display:none - display:block。
我正在使用这个 javascript:
<script language="JavaScript">
//here you place the ids of every element you want.
var ids=new Array('a1','a2','a3','thiscanbeanything');
function switchid(id){
hideallids();
showdiv(id);
}
function hideallids(){
//loop through the array and hide each element by id
for (var i=0;i<ids.length;i++){
hidediv(ids[i]);
}
}
function hidediv(id) {
//safe function to hide an element with a specified id
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById(id).style.display = 'none';
}
else {
if (document.layers) { // Netscape 4
document.id.display = 'none';
}
else { // IE 4
document.all.id.style.display = 'none';
}
}
}
function showdiv(id) {
//safe function to show an element with a specified id
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById(id).style.display = 'block';
}
else {
if (document.layers) { // Netscape 4
document.id.display = 'block';
}
else { // IE 4
document.all.id.style.display = 'block';
}
}
}
</script>
还有这个 HTML:
<p>Try these: <a href="javascript:switchid('a1');">show a1</a>
<a href="javascript:switchid('a2');">show a2</a>
<a href="javascript:switchid('a3');">show a3</a>
<a href="javascript:switchid('thiscanbeanything');">show 'thiscanbeanything'</a></p>
<hr/>
<div id='a1' style="display:block;">
<h2>Sample text:</h2>
<p><b>Jean-Paul Sartre, (1905-1980)</b> born in Paris in 1905...</p>
</div>
<div id='a2' style="display:none;">
<h3>More on JPS</h3>
<p>The conclusions a writer must draw from this position...</p>
</div>
<div id='a3' style="display:none;">
<p>Yet more content. This can be anything in here, html,
pictures.. flash ...</p>
</div>
<div id='thiscanbeanything' style="display:none;">
<h3>This content is in a div with id "thicanbeanything"</h3>
<p>Sartre is one of those writers for whom a determined...</p>
</div>
我需要将此 HTML 和 Javascript 放入这段代码中。来自核心电子商务的单品展示文件:
<?php /** the custom meta HTML and loop */ ?>
<div class="custom_meta">
<?php while (wpsc_have_custom_meta()) : wpsc_the_custom_meta();
if (stripos(wpsc_custom_meta_name(),'g:') !== FALSE){
continue;
}
?>
<strong><?php echo wpsc_custom_meta_name(); ?>: </strong><?php echo wpsc_custom_meta_value(); ?><br />
<?php endwhile; ?>
</div>
<?php /** the custom meta HTML and loop ends here */?>
一切都通过一个变量调用:$wpsc_query;。分享我们正在寻找的价值观:
[custom_meta_values] => Array ( [id] => 75 [product_id] => 6 [meta_key] => Product code [meta_value] => 123123123123 [custom] => 1 )
所以基本上我需要创建一个循环,将每个meta_key 和meta_value 保存在wordpress 后端的插件产品选项中。并创建一个循环来获取这些值,将它们放入 <div>'s 并将 javascript 的 <a> 链接放在那里。
附:我正在寻找类似的东西:
<?php
foreach ($wpsc_query->custom_meta_values['meta_key']) {
print '<div class="single_menus_wrapper">';
print '<a href="javascript:switchid("single_menu_' . custom_meta_$i . '");">show single_menus_title</a>';
print '<div id="single_menu_' . custom_meta_$i . '"><div class="single_menus_holder">';
print '<div class="single_menus_title">' . echo wpsc_custom_meta_name(). ':</div>';
print '<div class="single_menus_description">' . echo wpsc_custom_meta_value() . '</div>';
print '</div></a></div>';
}
?>
但是内容不会显示在页面上。主要是因为这段代码很可笑。任何帮助都会很棒,提供教程链接、建议、代码的一部分,或者至少是我应该在这里寻找的想法。
【问题讨论】:
-
你为什么要检查 netscape 和 IE4?
-
电子商务的 php 代码不是已经完成了你想要的吗?在while循环中迭代......
-
为什么你的
<script>标签来自Netscape和IE4时代?如果你使用 HTML5,使用<script>就足够了,否则使用<script type="text/javascript">。删除language属性。
标签: php wordpress plugins loops foreach