所以我查看了您的网站,当用户点击某个类别时,他们实际上会被带到该特定类别的网页。 因此,当前子类别不包含在主页中。
如果他们允许,您的第一个解决方案是在您的 CMS 中编辑菜单模板。
下面有一个解决方案/HACK,但更改 CMS 中的模板会更好
如果您添加此代码,当用户访问主页时,下面的脚本将告诉浏览器对所有具有子类别的类别进行 Ajax 调用。当每个分类页面源返回时,找到子分类列表,分别插入到首页的菜单中。
测试这段代码。您可以直接转到您网站的主页。然后,打开控制台并粘贴第一个函数,然后粘贴 onload 函数中的脚本。
//a simple function used to make an ajax call and run a callback with the target page source as an argument when successful
function getSubPageSource(url, successCallback)
{
var xhr = XMLHttpRequest();
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4 && xhr.status == 200)
{
//when source returned, run callback with the response text
successCallback(xhr.responseText);
}
};
xhr.open('GET', url, true);
xhr.send();
}
//wrap in onload event
window.onload = function() {
//find all categories with sub categories
var categories = document.getElementsByClassName('has-subcategories');
//loop through each category
for (var ii = 0, nn = categories.length; ii < nn; ii++)
{
//use a closure to pass in a static ii
(function(ii)
{
//get element
var element = categories.item(ii);
//find id
var id = element.getAttribute('data-category');
//find url
var href = element.getAttribute('href');
if (id && href)
{
//found
getSubPageSource(href, function(data)
{
//find sub categories
//trim off everything before where id shows up first
var substrSource = data.substr(data.indexOf('data-category="'+id+'"'));
//trim off the remaining of the category title
substrSource = substrSource.substr(substrSource.indexOf('</a>'));
//trim off where the next category is found
substrSource = substrSource.substr(0, substrSource.indexOf('home-categories-main'));
//trim off where the next category starts, leaving source of all sub categories
substrSource = substrSource.substring(0, substrSource.lastIndexOf('<a '))
//insert to main menu after the title
console.log(id, substrSource);
//create new node capable of having children
var newNode = document.createElement("span");
//insert new source into the new node
newNode.innerHTML = substrSource;
//insert new node after element
element.parentNode.insertBefore(newNode, element.nextSibling);
});
}
})(ii);
}
};
这个脚本可能只在HOME PAGE ONLY工作,因为我没有在你的其他页面上测试过。如果添加到其他页面,它可能会在各个类别页面上创建重复的子类别列表。
注意:第二种方法的一个巨大缺点是,每次有人访问您的主页时,他们的后台浏览器都会依次访问具有子类别的所有类别页面。这意味着您的服务器必须为每次主页访问提供多个页面。
我真的建议在 CMS 中找到一种方法来编辑菜单模板,而不是上面的脚本
粘贴上面的脚本,您的主页生成了必要的链接,如下面的屏幕截图所示。