[英]WordPress is not calling the default jQuery file
我正在嘗試為我的插件調用jQuery DataTable JS文件,該插件使用DataTable從數據庫中顯示查詢。 JS文件本地存儲在服務器上。
WordPress:v4.0.1 jQuery:v1.11.1 DataTable:v1.10.5
WordPress默認的jQuery文件沒有被調用。 當我注冊並調用托管在Google上的jQuery文件時,它似乎有效。 有沒有理由發生這種情況?
這是我到目前為止:
的functions.php:
function load_jquery_datatables() {
wp_enqueue_script('jquery');
wp_register_script( 'jquery-datatables', plugins_url('js/jquery.dataTables.min.js'), array('jquery'));
wp_enqueue_script('jquery-datatables');
}
index.php文件:
<?php add_action('wp_enqueue_scripts', 'load_jquery_datatables');?>
<script>
$(document).ready(function() {
$('#example').DataTable();
});
</script>
<table id="example" class="display">
<form>
<thead>
<tr>
<td style="text-align: center;"><input type="checkbox" id="selectall"></td>
<?php
foreach ( $wpdb->get_col( "DESC " . $table_name, 0 ) as $column_name )
{
echo '<th style="text-align: center;">' . $column_name . '</th>';
}
?>
</tr>
</thead>
<tfoot>
<tr>
<td style="text-align: center;"><input type="checkbox" id="selectall"></td>
<?php
foreach ( $wpdb->get_col( "DESC " . $table_name, 0 ) as $column_name )
{
echo '<th style="text-align: center;">' . $column_name . '</th>';
}
?>
</tr>
</tfoot>
<tbody>
<?php
foreach ( $results as $row )
{
echo '<tr>';
echo '<td><input type=\'checkbox\' class=\'check\' value=' . $id . '></td>';
echo '<td>' . $row->id . '</td>';
echo '<td>' . $row->status . '</td>';
echo '<td>' . $row->create_time . '</td>';
echo '<td>' . $row->start_time . '</td>';
echo '<td>' . $row->duration . '</td>';
echo '<td>' . $row->source . '</td>';
echo '<td>' . $row->source_id . '</td>';
echo '</tr>';
}
?>
</tbody>
</form>
</table>
自從我創建了一個WordPress主題以來,我已經有一段時間了,但我總是使用這種方法而且效果很好!
的functions.php
function custom_function_name() {
wp_enqueue_script("jquery");
wp_head(); ?>
<script type="text/javascript" src="<?php bloginfo("template_url"); ?>/js/yourScript.js"></script><?php
}
在您的HEADER.PHP NOT INDEX.PHP中采取行動
<head>
// Link stylesheets and page titles
// Call your function
<?php custom_function_name(); ?>
<script>
$(document).ready(function() {
$('#example').DataTable();
});
</script>
</head>
以下更改解決了該問題:
在index.php中更改jQuery:
<script>
$(document).ready(function() {
$('#example').DataTable();
});
</script>
以下內容:
<script>
jQuery(document).ready(function() {
$('#example').DataTable();
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.