【发布时间】:2017-06-14 05:10:00
【问题描述】:
我希望每次打开或加载帖子页面时运行我的 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(); ?>
【问题讨论】:
-
你有必要回声吗?您可以关闭 php
while ( have_posts() ) : the_post(); ?>,像往常一样输出脚本 html,然后再次开始打开 php 标签<?php get_template_part( 'content', get_post_format() );。如果您的脚本使用 php 变量,则在其中添加 php 标签:alert("<?php echo 'hello!'; ?>");
标签: javascript php jquery ajax wordpress