【问题标题】:How do I use jquery inside WordPress Loop?如何在 WordPress 循环中使用 jquery?
【发布时间】:2013-05-10 01:08:54
【问题描述】:

如何在 WordPress Loop 中使用 jquery 而不重复效果

<div class="post">
       <?php if ( have_posts() ) : 
       while ( have_posts() ) : the_post(); ?>
           <a class="post_image"  href="<?php the_permalink(); ?>" >
               <img  class="post_image_home"  src='<?php $thumb = get_post_custom_values('post_thumb'); echo $thumb[0]?>'   alt="postimg" />
           </a><!--post_image-->
       <?php endwhile; endif; ?></div><!--post-->

jquery:

    $j=jQuery.noConflict();
   $j('document').ready(function() {
      $j('.post').hover(function() {
             $j('.post_image').stop().animate({"margin-bottom":"10px",},"fast");
             },function(){
             $j('.post_image').stop().animate({"margin-bottom":"0px",},"fast");
      });            });

【问题讨论】:

    标签: jquery wordpress loops effect


    【解决方案1】:

    你不能把它添加到 JS 文件中吗?可能比在 PHP 文件中包含 JS 更好,这样会阻止它在每次 PHP 循环时运行。

    如果您必须在 PHP 文件中执行此操作,您可以在循环开始之前创建一个全局 JS 变量,并在运行您自己的 JS/jQuery 内容之前检查它。在循环中的 JS 中,更改该全局变量,使其不再运行。

    == 编辑 ==

    这是一些示例代码,用于在您的主题中包含一个单独的 JS 文件。

    这将在您的functions.php中:

    add_action('wp_enqueue_scripts', 'front_end_js_files');
    function front_end_js_files(){
        // shouldn't be needed, but to be safe
        wp_enqueue_script('jquery');
        // this is looking in the same dir as your functions.php
        wp_enqueue_script('my_own_file', get_bloginfo('template_url') . 'my_own_file.js');
    }
    

    然后在 my_own_file.js 中,粘贴您的 jQuery。请注意,您的 JS 现在将包含在每个页面中,因此您可能希望使 jQuery 选择器更具体一些。或者,在将脚本加入队列之前,在 PHP 中添加一些自定义页面检测。

    【讨论】:

    • 我需要一个例子我初学者XD请
    猜你喜欢
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    相关资源
    最近更新 更多