私は ajax 経由でコンテンツをロードする機能的なワードプレスのテーマを持っています。私が抱えている問題の 1 つは、ページが直接読み込まれると、ajax スクリプトが機能しなくなることです。たとえば、リンク構造は次のように機能します。www.example.com と about ページのリンクをクリックすると、リンクは www.example.com/#/about になります。しかし、スタンドアロン ページ www.example.com/about を直接読み込むと、このページからクリックされた他のリンクは www.example.com/about/#/otherlinks に変わります。このチュートリアルhttp://www.deluxeblogtips.com/2010/05/how-to-ajaxify-wordpress-theme.htmlからコードを少し変更しました。これが私のコードです。助けてくれてありがとう。
jQuery(document).ready(function($) {
var $mainContent = $("#container"),
siteUrl = "http://" + top.location.host.toString(),
url = '';
$(document).delegate("a[href^='"+siteUrl+"']:not([href*=/wp-admin/]):not([href*=/wp-login.php]):not([href$=/feed/]))", "click", function() {
location.hash = this.pathname;
return false;
});
$(window).bind('hashchange', function(){
url = window.location.hash.substring(1);
if (!url) {
return;
}
url = url + " #ajaxContent";
$mainContent.fadeOut(function() {
$mainContent.load(url,function(){
$mainContent.fadeIn();
});
});
});
$(window).trigger('hashchange');
});