代码的主要问题是没有将JS包装在<script>
要素
<?php
/*
Template Name: Price Chart
*/
/**
* Enqueue the table sorter script
*/
function load_table_sorter_scripts()
{
//wp_enqueue_style( \'style-name\', get_stylesheet_uri() );
wp_enqueue_script( \'tablesorter\', get_template_directory_uri() . \'/js/jquery.tablesorter.min.js\', array(\'jquery\'), \'1.0.0\', true );
}
add_action( \'wp_enqueue_scripts\', \'load_table_sorter_scripts\' );
// Now start outputting the HTML
get_header();
global $gp_settings;
?>
<!-- HTML body content -->
<!-- You can put this anywhere in the body -->
<script>
jQuery(document).ready(function()
{
jQuery("#priceTable").tablesorter();
});
</script>
<?php get_footer(); ?>
现在这不是最干净的。要使其更干净,请执行以下操作:
将连接tablesorter的部分移动到自己的Javascript文件中,将enqueue脚本函数移动到functions.php
归档并检查调用的页面
functions.php
//...
function load_table_sorter_scripts()
{
// Check that it is the Price Chart page template (you may need to change the name to match the file name).
if ( is_page_template(\'price-chart.php\') ) {
wp_enqueue_script( \'tablesorter\', get_template_directory_uri() . \'/js/jquery.tablesorter.min.js\', array(\'jquery\'), \'1.0.0\', true );
wp_enqueue_script( \'tablesorter-price-chart\', get_template_directory_uri() . \'/js/price-chart-table.js\', array(\'jquery\', \'tablesorter\'), \'1.0.0\', true );
}
}
add_action( \'wp_enqueue_scripts\', \'load_table_sorter_scripts\' );
js/price-chart-table.js
jQuery(document).ready(function()
{
jQuery("#priceTable").tablesorter();
});