我希望每次打开或加载帖子页面时都会运行我的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(); ?>
答案 0 :(得分:1)
你没有逃避“你好”的文字:
alert("hello!");
请改为尝试:
alert('hello!'); // or alert(\"hello!\");
答案 1 :(得分:1)
最好的方法可能是使用fopen
e.g。
<?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路径(这是缺点..它是一个很长的路径!..很多反斜杠..)
答案 2 :(得分:1)
我认为在single.php文件中使用你的脚本循环或尝试其他方式。
if( jQuery(".single").hasClass(".single-post") ){
alert("hello!"); // whatever you want to process.
}
在主js文件中使用此代码。我认为它会对你有所帮助