【问题标题】:How to write a forerach loop in this situation?在这种情况下如何编写foreach循环?
【发布时间】: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_keymeta_value 保存在wordpress 后端的插件产品选项中。并创建一个循环来获取这些值,将它们放入 &lt;div&gt;'s 并将 javascript 的 &lt;a&gt; 链接放在那里。


附:我正在寻找类似的东西:

<?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循环中迭代......
  • 为什么你的&lt;script&gt;标签来自Netscape和IE4时代?如果你使用 HTML5,使用&lt;script&gt; 就足够了,否则使用&lt;script type="text/javascript"&gt;。删除 language 属性。

标签: php wordpress plugins loops foreach


【解决方案1】:

通过查看您的源代码,我认为实际上您希望产品详细信息页面上的“标签功能”,

所以在那种情况下(如果我理解正确的话)

您可以使用Wordpress Post-tabs,请参见第三张屏幕截图并相应地输入选项卡名称。

【讨论】:

  • 如果我知道它被称为“标签功能”,那么我就不必做所有这些工作了。我确实设法做到了,但我能想到的最愚蠢的方式是: if(n==1) { div1.style.display = "block"; div2.style.display = "无"; div3.style.display = "无"; div4.style.display = "无";制作
    和 $i++;原谅我编程之神。无论如何,感谢 Denish 向我展示了这个漂亮的插件,下次我会使用它,现在我所做的事情对我来说非常有用。
猜你喜欢
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 2023-02-04
  • 2012-04-08
  • 2021-06-19
  • 2021-05-21
  • 2013-11-03
相关资源
最近更新 更多