【问题标题】:Include script in infinite scroll Wordpress Posts在无限滚动 Wordpress 帖子中包含脚本
【发布时间】:2018-04-02 10:57:06
【问题描述】:

我们目前正在为博客文章使用 Ajax 无限滚动。但我们希望在每篇博文中都包含脚本(javascript)。一切正常,除了脚本只在博文的最顶部显示一次。

这里是 singles.php 循环的 sn-p,它已针对 Ajax 无限滚动标准进行了修改:

<?php while ( have_posts() ) : the_post(); ?>
<?php if (et_get_option('divi_integration_single_top') <> '' && et_get_option('divi_integrate_singletop_enable') == 'on') echo(et_get_option('divi_integration_single_top')); ?>
    <?php
    echo do_shortcode('[ajax_load_more cache="true" cache_id="2869844236" cta="true" cta_position="after:1" css_classes="call-to-actions call-to-actions-js" images_loaded="true" post_type="post" repeater="default" previous_post="true" previous_post_id="'. get_the_ID() .'" posts_per_page="1" button_label="Previous Post"]');
    ?>

<?php endwhile; ?>

这里是转发器模板的 sn-p,其中包含一个简单的脚本代码。

<article id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post' ); ?>>
  <script>
    document.write("This text is displayed using a simple javascript.");
  </script>
  <div class="et_post_meta_wrapper">
    <h1><?php the_title(); ?></h1>
    <?php the_post_thumbnail('hero', array('alt' => get_the_title())); ?>
  </div>
  <div class="entry-content">
    <?php
    do_action( 'et_before_content' );

    the_content();

    wp_link_pages( array( 'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'Divi' ), 'after' => '</div>' ) );
    ?>
  </div>
</article>

不知道为什么脚本只显示一次。当我把它放在每篇博文的标题顶部时。

【问题讨论】:

    标签: wordpress wordpress-theming infinite-scroll


    【解决方案1】:

    document.write() 将在您的 ajax 请求转发器模板时间接调用 document.open() 以打开新的输出流。下次您滚动更多时,write() 方法不起作用,因为先前的输出流当前是打开的。您应该关闭流。

    你可以试试这个方法:

    document.open();
    document.write("This text is displayed using a simple javascript.");
    document.close();
    

    document.write() 对生产来说是一种不好的做法。您应该只将其用于测试。

    更好的解决方案:

    <p id="output"></p>
    
    <script>
    document.getElementById("output").innerHTML = "This text is displayed using a simple javascript.";
    </script>
    

    编辑:

    要在每次完成 ajax 请求后显示文本,我们需要一个唯一的 id 来将文本附加到元素。

    在我上面的建议中

    id 必须是唯一的,才能从 JavaScript 中重复文本。

    解决方案:

    我们将使用 WordPress 来获得一点帮助。 WordPress 对您创建的所有帖子都有唯一的 ID。

    the_ID(); WordPress While 循环中的方法返回帖子 ID。因此,在我们的 P 元素中附加相同的 id,并通过获取元素的新 id 来附加我们的文本。

    <p id="output-<?php the_ID(); ?>"></p>
    
    <script>
      var idname = document.getElementById("output-<?php the_ID(); ?>");
      idname.innerHTML = "This text is displayed using a simple javascript.";
    </script>
    

    这将在每个帖子中附加文本。

    【讨论】:

    • 谢谢,谢谢。我一定会采纳你的建议。但是我如何让它在每个帖子的循环中重复?
    • 我认为你的转发器模板进入了 WordPress while 循环,所以它应该重复,除非你有发布或做 ajax 请求。
    • Repeater 模板工作正常...它按预期循环所有/显示所有帖子。我的问题是我包含在转发器模板中的脚本。最特别的是在博客文章标题上方。它不会显示在每篇博文中,而是仅显示在第一篇博文中。
    • 你试过上面的建议了吗?正如我在上面解释的 write() 方法是如何工作的(如果没有正确打开/关闭流,则只有一次),您应该硬编码一些文本或使用 innerHTML 并测试滚动。
    • 是的,我的新转发器模板现在看起来像这样。screencast.com/t/LDkxZiAAUlEL 但它仍然在第一篇博文而不是每一篇博文上显示一次脚本
    猜你喜欢
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 2014-10-27
    相关资源
    最近更新 更多