有几种方法可以实现这一点。我认为以下方法是最好的使用方法。
Method 1
只需将脚本代码直接添加到函数文件中,并将该函数挂接到
wp_enqueue_scripts
function enqueue_my_script() {
?>
<script>
(function($){
$("#to").autocomplete({
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
function split( val ) {
return val.split( /,\\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#tags" )
// don\'t navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "ui-autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
})(jQuery);
</script>
<?php
}
add_action( \'wp_enqueue_scripts\', \'enqueue_my_script\', 999 );
Method 2
创建一个名为“my script”的文件。js\'位于根目录或js文件夹中。打开它并添加jquery,而不使用
<script></script>
标签。下面是要添加到js文件中的代码
(function($){
$("#to").autocomplete({
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
function split( val ) {
return val.split( /,\\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#tags" )
// don\'t navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "ui-autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( $.ui.autocomplete.filter(
availableTags, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
})(jQuery);
现在,使用
wp_enqueue_script()
, 注册jquery并将其挂接到
wp_enqueue_scripts
钩
function enqueue_my_script() {
wp_enqueue_script( \'script-name\', get_template_directory_uri() . \'/js/my-script.js\', );
}
add_action( \'wp_enqueue_scripts\', \'enqueue_my_script\', 100 );
记住要改变
get_template_directory_uri()
到
get_stylesheet_directory_uri()
如果您使用的是子主题。
更多阅读,请查看我提供的链接。