random reason on refresh

时间:2013-06-09 作者:Kev Hopwood

我正在编辑侧边栏小部件,并为未登录或注册的用户添加了一个。小部件要求用户注册或登录,我想添加一个随机原因(引号),说明他们为什么应该加入,每次重新加载页面后,这个原因都会改变。

我选择在网站中使用“文本”,它似乎没有加载我输入的任何JavaScript代码。它显然没有读到它们。

有人知道我怎么做吗?

如果我使用了错误的小部件类型,那么哪个小部件将读取和显示JavaScript代码?

还是有一种无JavaScript的方法可以做到这一点?

非常感谢。

站点链接:PLEASE CLICK HERE

2 个回复
SO网友:s_ha_dum

你说你是。。。

。。。想要添加一个随机的原因(引号),说明他们为什么应该加入,这将在每次重新加载页面后更改

您似乎试图通过在文本小部件中放置Javascript来实现这一点。我想这就是你所说的“站点中的文本”的意思。

基于这种描述,您不需要Javascript。您需要一个新的边栏小部件。

class Quote_Login extends WP_Widget {
  /*constructs etc*/
  function __construct($id = \'quologin\', $descr = \'Quote Login\', $opts = array()) {
    $widget_opts = array();
    parent::__construct($id,$descr,$widget_opts);
  }

  function widget() { 
    $ar = array(
      \'“Nothing is as easy as it looks.”\',
      \'“Everything takes longer than you think.”\',
      \'“Anything that can go wrong will go wrong.”\',
      \'“If there is a possibility of several things going wrong, the one that will cause the most damage will be the one to go wrong.”\',
      \'“If there is a worse time for something to go wrong, it will happen then.”\',
      \'“If anything simply cannot go wrong, it will anyway.”\',
      \'“If you perceive that there are four possible ways in which a procedure can go wrong, and circumvent these, then a fifth way, unprepared for, will promptly develop.”\',
      \'“Left to themselves, things tend to go from bad to worse.”\',
      \'“If everything seems to be going well, you have obviously overlooked something.”\',
      \'“Nature always sides with the hidden flaw.”\',
      \'“Mother nature is a bitch.”\',
      \'“It is impossible to make anything foolproof because fools are so ingenious.”\',
      \'“Whenever you set out to do something, something else must be done first.”\',
      \'“Every solution breeds new problems.”\',
      \'“Trust everybody … then cut the cards.”\',
      \'“Two wrongs are only the beginning.”\',
      \'“If at first you don’t succeed, destroy all evidence that you tried.”\',
      \'“To succeed in politics, it is often necessary to rise above your principles.”\',
      \'“Exceptions prove the rule … and wreck the budget.”\',
      \'“Success always occurs in private, and failure in full view.”\',
    );
    $quote = $ar[array_rand($ar,1)]; ?>
    <p><b>It looks like you haven’t logged in or have not yet registered.<br>
To receive the most accurate results and member benefits, login below or click register to become a member.</b></p>
    <center>
      <form action="wp-login.php" method="LINK">
        <b><br><input type="submit" value="LOGIN"><br></b>
      </form>
    </center>
    <p></p>
    <b><p>Why Become A Member?</p>
    <form name="random">
      <input type="text" value="" size="78" name="random" />
    </form>
    <?php 
      // here is your quote
      // the curly quotes you apparently want won\'t work in the form input
      // but I don\'t know why you are shoving this into an input anyway
      echo $quote; ?>
    <p></p><center>
      <form action="wp-register.php" method="LINK"><br>
        <input type="submit" value="REGISTER"><br>
      </form>
    </center><p></p>
    </b><p><b>XCLO</b></p> <?php
  }
}
add_action(\'widgets_init\', function() { register_widget("Quote_Login"); } );
只有在需要定期更改引号而不需要重新加载页面时,才需要Javascript。

我没有纠正你的基本标记(很多),但有几件事让我觉得奇怪,比如使用表单创建本质上是链接的内容,以及使用deprecated in HTML4 and unsupported in HTML5 elements like <center>

SO网友:Kev Hopwood

通过安装“HTML JAVASCRIPT ADDER”插件对其进行排序。

谢谢

结束

相关推荐

Shortcode in AJAX popup

我试图使用Ultimate Facebook插件的一个短代码在AJAX弹出窗口中显示FB connect按钮,但似乎无法正常工作。我搜索并使用了djb在这个问题上给出的解决方案-Why might a plugin's 'do_shortcode' not work in an AJAX request?, 但它只起了部分作用。理想情况下,我希望用户能够使用FB登录,然后将列表保存到她的帐户。我想使用插件而不是嵌入直接代码,因为当有人使用FB connect时,也会在wordpre