18910140161

JavaScript-如何在标签上加载Shopify博客视图/模板,即使该标签没有文章?-堆栈溢出

顺晟科技

2022-10-18 13:32:07

99

预期结果:

正如标题所说,我试图让我的模板的自定义英雄部分加载,即使他们没有一个标签的文章。

实际结果:

现在

的行为是间歇性的。有些标签会加载英雄,但大多数不会。

错误:

在控制台上的那些不工作,我收到一个'得到404 '错误和我的404页面加载。在那些正在工作的,我没有得到任何错误。

HTML:

<div id="blog-hero">
    <!-- blog hero goes here -->
</div>

<div id="article-index-background">
    <div id="article-section" class="width g-flex">
        <!-- paginate articles here -->
    </div>
</div>

JS:

// get the hero
const pathName = window.location.pathname;
let blog_div = document.getElementById('blog-hero');
let blogName = pathName.split('/')[2];
let tagName = pathName.split('/')[4];

let request = new XMLHttpRequest();
request.open('GET', `/?section_id=blog--hero-${blogName}-blog--${tagName}`)

request.onload = () => {
    if(request.readyState === request.DONE) {
        if (request.status === 200) {
             blog_div.innerHTML = request.responseText;
         }
    }
};

request.send(null);

// load the articles, paginate on screen width
let screenWidth = window.innerWidth;
let articleSection = document.getElementById('article-section');

if (screenWidth < 600) {
    articleSection.innerHTML = `
    {% if blog.articles %}
        {% paginate blog.articles by 3 %}
            {% for article in blog.articles %}
                // some html
            {% endfor %}
        {% endpaginate %}
    {% endif %}
    `

} else if (screenWidth >= 600) {
    articleSection.innerHTML = `
    {% if blog.articles %}
        {% paginate blog.articles by 6 %}
            {% for article in blog.articles %}
                // some html
            {% endfor %}
        {% endpaginate %}
    {% endif %}
    `

}

顺晟科技:

为了解决这个问题,我实现

了一个简单的解决方法,即在博客上发表一篇标题为“隐藏的文章”的文章。并在前端隐藏任何带有该标题的博客文章。有点像黑客,但很有效。

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航