我希望我的jQuery脚本在每次打开或加载帖子页面时都运行。我尝试echo
在脚本中使用但无法正常工作
我应该在哪里放置脚本以使其运行?
single.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<?php
/**
* The template for displaying all single posts and attachments
*
* @package WordPress
* @subpackage Twenty_Fifteen
* @since Twenty Fifteen 1.0
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post();
echo "
<script type=\"text/javascript\">
alert("hello!"); //placeholder script
</script>
";
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
// Previous/next post navigation.
the_post_navigation( array(
'next_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Next', 'twentyfifteen' ) . '</span> ' .
'<span class="screen-reader-text">' . __( 'Next post:', 'twentyfifteen' ) . '</span> ' .
'<span class="post-title">%title</span>',
'prev_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Previous', 'twentyfifteen' ) . '</span> ' .
'<span class="screen-reader-text">' . __( 'Previous post:', 'twentyfifteen' ) . '</span> ' .
'<span class="post-title">%title</span>',
) );
// End the loop.
endwhile;
?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
最好的方法可能是使用fopen
例如
<?php
$filename = "https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js";
$myfile = fopen($filename, "rb");
if (FALSE === $myfile) {
exit("Failed to open stream to URL");
}
while (!feof($handle)) {
$contents = stream_get_contents($myfile, 8192));
}
fclose($myfile); //close the file again when necessary
?>
您也可以反斜杠反斜杠并以传统方式回显脚本
<?php echo "<script type=\"text/javascript\" src=\"jquery.min.js\"></script>; ?>
请注意,我尚未完成jquery路径(这是不利的一面。这是一条漫长的路!..很多反斜杠..)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句