JavaScript 不会在内容区域 wordpress 之外工作

JavaScript wont work outside of content area wordpress

本文关键字:wordpress 工作 区域 JavaScript      更新时间:2023-09-26

基本上我正在使用wordpress,我希望我的分享链接随着用户向下滚动而跟随帖子。我可以让它完全按照我想要的方式工作,除了我已将 JavaScript 直接附加到内容中.php这意味着当页面显示多个帖子时,JavaScript 会在页面上多次加载。这导致粘性功能仅适用于前几个帖子。我尝试将我的javascript移动到标题,但没有运气。请有人帮帮我!!以下是我的内容.php文件,仅适用于前几篇文章。

<script src="/wp-content/themes/dw-minion/assets/css/jstick/jquery.js"></script><script src="/wp-content/themes/dw-minion/assets/css/jstick/jquery.stickem.js"></script>
    <script>
        $(document).ready(function() {
            $('.container').stickem();
        });
    </script><div class="title-wrapper"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><a href="<?php the_permalink(); ?>#comments" title="<?php comments_number( 'No Comments', '1 Comment', '% Comments' ); ?>"><div class="commentnumber"><?php comments_number( '0', '1', '%' ); ?></div></a></div><div class="container"><div class="stickem-container"><div class="thelinks stickem"><div class="sharelinks"><div class="sharepinterest"><?php echo get_simple_local_avatar( $id_or_email, $size, $default, $alt ); ?></div><a href="http://www.facebook.com/sharer/sharer.php?s=100&p[url]=<?php the_permalink(); ?>&p[images][0]=http://www.otlcampaign.org/sites/default/files/journey-for-justice-mlk-memorial.jpg&p[title]=<?php the_title(); ?>&p[summary]=Click+to+enlarge"><div class="sharefacebook"></div></a><a href="http://twitter.com/home?status=<?php the_title(); ?>+<?php the_permalink(); ?>"><div class="sharetwitter"></div></a><div class="sharegoogle"></div></div></div>
    <div class="post-wrapper"><div class="entry-content"> <a href="<?php the_permalink(); ?>"><?php the_content(); ?></a>
    </div></div></div>
    </div><a>

尝试将其粘贴在页脚中。

如果这不起作用,请在控制台中查找容器,您可以从

$(document).ready

$(".container").change
// or whatever the css selector for the container is

这样,stickem() 将在容器更改的那一刻触发,也就是加载新帖子。

您需要

将代码移动到页脚.php文件中,并进行以下更改:

<script src="/wp-content/themes/dw-minion/assets/css/jstick/jquery.stickem.js"></script>
jQuery(document).ready(function($) {
  $('.container').stickem();
});

默认情况下,WordPress启用了jQuery无冲突,因此您必须使用jQuery而不是使用$。这也意味着您不需要包含:

<script src="/wp-content/themes/dw-minion/assets/css/jstick/jquery.js"></script>