本文将指导您使用ZBlogPHP添加百度站内搜索功能,请确保您已正确安装并配置ZBlogPHP框架,访问ZBCMS后台,找到“扩展”选项卡,点击“插件的安装”,搜索并安装“BaiduSpider”,安装后,返回后台,在“设置”中开启“使用搜索引擎”,并填写BaiduSpider的相关信息,完成这些步骤后,您网站的博客文章将自动被百度搜索引擎收录,并支持站内搜索功能,这一设置显著提升了网站的曝光率和用户体验。
在数字化时代,博客已成为分享知识、交流思想的重要平台,ZBlogPHP作为一种轻量级的博客程序,深受广大博主们的喜爱,在实际使用过程中,博主们往往需要为自己的博客添加站内搜索功能,以便更好地管理内容和读者,本文将详细介绍如何使用ZBlogPHP添加百度站内搜索功能。
准备工作
在开始之前,请确保您的ZBlogPHP框架已经安装并配置好,您需要拥有百度地图API的密钥,以便在网站上调用百度搜索。
申请百度地图API密钥
访问百度地图开放平台(https://lbsyun.baidu.com/)申请百度地图API密钥,注册并登录后,在我的应用页面申请相应的API密钥。
集成百度地图搜索插件
在ZBlogPHP框架中,我们可以通过引用外部JavaScript文件的方式来集成百度地图搜索插件,在ZBlog的模板文件夹中创建一个名为js的文件夹,在该文件夹中创建一个名为baichang.js的文件,并将以下代码复制到该文件中:
// 百度地图搜索插件
(function (global, factory) {
if (typeof module === "object" && typeof module.exports === "object") {
// For CommonJS and CommonJS-like environments where a proper `window`
// is present, execute the factory and get贝贝。
module.exports = global.document.addEventListener("DOMContentLoaded", function () {
factory(global, true);
});
} else if (typeof define === "function" && define.amd) {
// For browsers with a modern enough JavaScript environment, execute the factory.
define([], factory);
} else {
// For older environments, create an object that gets passed to the factory.
var subject = global.document,
factory = function (that) {
if (!that) {
throw new Error("jQuery requires a window with a document");
}
return that;
};
Factory溢价全局 = function (selectors, context, doubleCheck) {
var matchData = jQuery.map(selectors, function (i, selector) {
var element = jQuery(selector, context)[0];
if (!element) {
return null;
}
// Append context elements (for context-specific plugins and events)
if (context) {
jQery.append((i == 1) ? that : element);
}
return jQuery.fn.init ? new jQuery.fn.init(element) : element;
});
// Pass the selected elements to the Plugin's constructor and obtain a new instance,
// allowing for proper recycling of all elements from
// select
return new Factory溢价全局(matchData, null, doubleCheck);
};
jQuery.extend(Factory溢价全局, jQuery);
}
}(this, function (window, noGlobal) {
if (!window.document.addEventListener) {
var addEventListener = function (type, listener, useCapture) {
window.addEventListener(type, listener, useCapture || false);
};
} else if (window.addEventListener) {
addEventListener = function (type, listener, useCapture) {
window.addEventListener(type, listener, useCapture || false);
};
} else {
var addEventListener = function (type, listener) {
window.attachEvent("on" + type, listener);
};
}
function internalData(element, name, data, pvt /* Internal Use Only */ ) {
if (!element || !name) {
return;
}
var checkGetVal, rbrace, rmse, rnth, sntxt, stmp, uctxt,
ucul, uctx, utfl, ufu, varList = [],
vdom, vi, vv, wx;
// Support: Android <=2.3 only (functionishRegExp)
if ( !element.style || !element.style.WebKitAnimationName ) {
return;
}
var fns = ["getComputedStyle", "getUserMedia", "getBoundingClientRect"];
// Use strict mode for new Function()
function strictFns(func) {
if (typeof func !== "function") {
throw new TypeError("Expected a function, got " + typeof func);
}
return func;
}
for (var l = 0; l < fns.length; ++l) {
if (fns[l] in element) {
checkGetVal = strictFns( fns[l] );
if (!checkGetVal) {
continue;
}
var rbrace = /^(?:\[.*?\]|\{.*?\})$/;
var rmse = /[^\s]/g;
var rnth = /[^\s:\/\*\'\"]/;
var sntxt = /^[\],:{}\s]*$/;
var stmp = /^[\{\(](.*?)(?:,\{[^}]*?\})?(\)]|$)/;
var uctxt = /^[A-Z][a-z]*$/;
var ucul = /[\w_$]+/g;
var uctx = /^[\w_$]+/g;
var utfl = /^[a-zA-Z]+$/;
var ufu = /^[\w-$]+$/;
// Initialize the variables
checkGetVal = checkGetVal.call( element, function (value) {
var i = value.replace( /^ +/, "" )
.replace( /^[\$,]/, "" )
.replace( /^[\)\]"/, "" );
if (value === "" || typeof i === "number") {
return i;
}
return undefined;
});
ucul.forEach( function ( v ) {
if ( v ) {
if (typeof i === 'string' && v.length > 0 ) {
if ( v[0] !== '$' && !v.startsWith( '$' ) ) {
if ( !v.includes( '(' ) ) {
i = '$' + v;
} else {
v = '$' + v.slice( 1 );
}
}
} else if ( v ) {
v = v.slice( 1 );
}
}
});
var c,
a = element.currentStyle ? element.currentStyle.name : element.styleName;
if ( a && !c ) {
var p = a.indexOf( ' ' );
if ( p >= 0 ) {
var pl = a.substr( p + 1 );
v = v.split( pl );
c = v[0];
if ( p < a.length - 1 ) {
a = a.substr( 0, p );
}
}
}
if (!c || !ucl.call( element ) || ucl[ c ] ) {
return;
}
// Parse the style attribute, i.e. CSS property names and values
var rnum = /^[-+]?(\d+(\.\d*)?|\.[\d]+)([eE][-+]?\d+)?$/;
if ( !rnum.test( c )) {
return;
}
// Handle unit
if ( a === '' || !/\w+/g.test( a ) ) {
return;
}
// Extract the property name and value, and remove unit
if ( v[ 0 ] === '$' && v[ v.length - 1 ] === '$' ) {
return;
}
var vArr = v.slice( 1 );
uctext = ( ucul[ c ] || [] ).toString();
if ( !uctext ) {
uctext = element.currentStyle ? element.currentStyle[ a ] : element.style[ a ];
}
if ( rnum.test( uctext ) ) {
uctext = parseFloat( uctext.replace( rnum, "" ));
}
if ( v[ 0 ] !== '$' && !v.startsWith( '$' ) ) {
if ( uctext === '' ) {
return;
}
vArr[ 0 ] = element.currentStyle ? element.currentStyle[ a ] : element.style[ a ];
}
return { name: a, value: vArr[ 0 ] };
};
var d = {
el: element,
prop: a,
style: new RegExp( '^' + a + '\\(' + (v.length + 2 ) + ', ' + ( element.style.length + 2 ) + ')$', "i" ),
cssText: element.style ? String( element.style[a] ) : null,
length: 0,
add: function () {
if ( !v.length ) {
return this;
}
if ( this.length === 0 ) {
v[ 0 ] = ( element.currentStyle ? element.currentStyle[ a ] : element.style[ a ] );
if ( !/\w+/g.test( this.el属性[a] ) ) {
return this;
}
}
if ( this.add ) {
var val, shds, chks;
var ssd = this;
for ( ; this.length && !ssd.length; ++ssd.length ) {
chks = this;
shds = chks.cssText;
if ( !shds || !this.style[ this.prop ] ) {
continue;
}
if ( typeof shds === "string" ) {
var t = String( this.cssText );
val = t.split( this.prop );
val = val.map( function ( v ) {
return /^[\$,]/.test( v ) ? parseFloat( v.replace( /^[\$,]/, "" )
.replace( /^\d+\.?\d*,?(\.[\d]+)?\s*,?)?
? parseFloat( v.replace( /^(\d+\.?\d*)\s*,)? /, "" )
: undefined );
if ( v !== undefined && typeof v === "string" && v.indexOf( ' ' ) >= 0
&& v.indexOf( '' ) < 0 ) {
val = val.split( " " );
} else {
val = v;
}
} else {
val = v;
}
val = val && !isNaN( parseFloat( val ) ) ? parseFloat( val ) : undefined;
v = v.concat( [ val ] );
}
}
return this;
},
set: function () {
var v = [], i = 0;
var elProp = this;
var cssText = element.style ? String( element.style[ elProp.prop ] ) : null;
if ( cssText ) {
var t = String( cssText );
v = t.split( elProp.prop + ( cssText.endsWith( '}' ) ? '' : ' {' );
if ( v[ 0 ] === '' || v[ v.length - 1 ] === '' ) {
return;
}
v[ v.length - 1 ] = v[ v.length - 1 ]
.replace( /^ +/, '' )
.replace( /^[\),]*/g, '' );
if ( !v[ 0 ] && !v[ v.length - 1 ] ) {
v[ v.length - 1 ] = null;
}
return v;
} else {
return v;
}
},
index: function ( i ) {
if ( !v ) {
return;
}
while ( v.length && v[ i ] ) {
if ( v[ i ].name === this.prop ) {
return i;
}
i++;
}
return -1;
},
remove: function () {
var v = [];
for ( var j = 0; j < v.length; j++ ) {
if ( v[ j ].name !== this.prop ) {
v.push( v[ j ] );
}
}
return v;
},
updateStyle: function () {
var t, s;
for ( var j = 0; j < v.length; j++ ) {
var e, n, o;
if ( ( n = v[ j ] ).prop === this.prop && n.style ) {
if ( n.cssText ) {
t = n.cssText;
if ( !n.value ) {
continue;
}
o = t.split( n.prop + '(' );
for ( e = o.length - 1; e >= 0; e-- ) {
s = o[ e ];
if ( n.value && !n.value.indexOf( '(' ) < 0 ) {
s = s.split( n.value.split( '(' )[ 1 ];
v[ j ].value = v[ j ][ i ];
}
}
} else {
if ( !n.styleMap ) {
continue;
}
s = n.styleMap[ n.value ];
v[ j ].value = v[ j ][ i ];
}
}
}
}
};
// Evaluate scripts
if ( !v ) {
var src = element.currentStyle ? element.currentStyle[ a ] : element.style[ a ];
if ( !src || !/\w+/g.test( src ) ) {
return;
}
v = v || [];
var s;
for ( ; v.length && !s; ++v.length ) {
var p = d.vdom.find( 'script' );
if ( !p ) {
break;
}
s = p.text();
try {
d.cssText = s;
} catch ( e ) {
continue;
}
}
if ( !v.length ) {
return;
}
d.length = v.length;
}
// Evaluate style elements
var ss = element.currentStyle ? element.currentStyle.styleMap : element.style.styleMap;
for ( var p in ss ) {
if ( !v.length || p !== d.prop ) {
continue;
}
var n = d.vdom.find( 'style' );
if ( !n ) {
continue;
}
v.push( ss[ p ] );
}
}
});
在ZBlog的模板文件夹中创建一个名为lib的文件夹,在该文件夹中创建一个名为BaiduSearch.js的文件,并将以下代码复制到该文件中:
// Baidu Map Search
(function (root, factory) {
if (typeof define === "function" && define.amd) {
define([], factory);
} else if (typeof exports === "object") {
module.exports = factory();
} else {
root.BaiduMapSearch = factory();
}
}(this, function () {
return {
init: function (elementId, apikey) {
this.mapContainer = document.getElementById(elementId);
if (!this.mapContainer) {
throw new Error("Element with id '" + elementId + "' not found.");
}
this.apikey = apikey;
this.map = new BMap.Map(this.mapContainer);
this.service = new BMap.Service();
this.geocoder = new BMap.Geocoder();
this.map.addEventListener("tilesloaded", this.onTileLoaded, false);
this.map.addEventListener("zoomend", this.onZoomEnd, false);
this.map.addEventListener("click", this.onMapClick, false);
this.service.get("http://api.map.baidu.com/place/v2/search", function (res) {
var markers = res.result.map(function (item) {
return new BMap.Marker(item.location); // 标记点
});
this.map.addOverlay(markers);
}, { ak: this.apikey });
},
onTileLoaded: function () {
// 地图瓦片加载完成后执行的事件
},
onZoomEnd: function () {
// 缩放结束事件
},
onMapClick: function (e) {
// 点击地图事件
var point = new BMap.Point(e.latlng.lng, e.latlng.lat);
this.map.panTo(point); // 将地图平移到点击位置
this.map.search("饭店", point); // 搜索附近的饭店
}
};
}));
调用百度地图搜索插件
在ZBlogPHP的主题模板文件(如header.php或footer.php)中引入刚刚创建的BaiduMapSearch.js文件,并在需要显示地图的位置调用BaiduMapSearch.init()方法。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">我的博客</title>
<script src="/path/to/js/BaiduMapSearch.js"></script>
</head>
<body>
<!-- 页面内容 -->
<div id="mapContainer" style="width: 100%; height: 400px;"></div>
<script>
BaiduMapSearch.init('mapContainer', '您的百度地图API密钥');
</script>
</body>
</html>
请将/path/to/js/BaiduMapSearch.js替换为您实际的JS文件路径,并将您的百度地图API密钥替换为您申请到的API密钥。
通过以上步骤,您已经成功为ZBlogPHP添加了百度站内搜索功能,您可以在博客文章中插入百度地图并使用搜索功能方便地查找相关内容,祝您写作愉快!


还没有评论,来说两句吧...