为什么相关的 post 小部件只出现在页脚部分,而不是侧边栏?

Why is the related post widget only appearing in the footer secton, but not the sidebar?

我正在为我的 Blogger 博客使用相关的 post 小部件。当我在页脚中安装小部件时,它显示正确,但当我尝试在侧边栏中显示它时,注意到出现 - 只出现一个空白小部件。

这是我正在使用的代码,为什么小部件没有显示?:

<!--RelatedPostsStarts-->
<script language='JavaScript'>
//<![CDATA[
var relatedTitles = new Array();
var relatedTitlesNum = 0;
var relatedUrls = new Array();

function related_results_labels(json) {
    for (var i = 0; i < json.feed.entry.length; i++) {
        var entry = json.feed.entry[i];
        relatedTitles[relatedTitlesNum] = entry.title.$t;
        for (var k = 0; k < entry.link.length; k++) {
            if (entry.link[k].rel == 'alternate') {
                relatedUrls[relatedTitlesNum] = entry.link[k].href;
                relatedTitlesNum++;
                break;
            }
        }
    }
}

function removeRelatedDuplicates() {
    var tmp = new Array(0);
    var tmp2 = new Array(0);
    for(var i = 0; i < relatedUrls.length; i++) {
        if(!contains(tmp, relatedUrls[i])) {
            tmp.length += 1;
            tmp[tmp.length - 1] = relatedUrls[i];
            tmp2.length += 1;
            tmp2[tmp2.length - 1] = relatedTitles[i];
        }
    }
    relatedTitles = tmp2;
    relatedUrls = tmp;
}

function contains(a, e) {
    for(var j = 0; j < a.length; j++) if (a[j]==e) return true;
        return false;
}

function printRelatedLabels() {
    var r = Math.floor((relatedTitles.length - 1) * Math.random());
    var i = 0;
    document.write('<ul>');
    while (i < relatedTitles.length && i < 6) {
        document.write('<li><a href="' + relatedUrls[r] + '">' + relatedTitles[r] + '</a></li>');
        if (r < relatedTitles.length - 1) {
            r++;
        } else {
            r = 0;
        }
        i++;
    }
    document.write('</ul>');
}
//]]>
</script>
#related-posts {clear: both;background-color:#000;}
#related-posts ul{margin: 20px 0; padding: 0;list-style: none;}
#related-posts ul li {border-top: 1px solid #333;border-bottom: 1px solid #111;}
#related-posts ul li:first-child {border-top: none;}
#related-posts ul li:last-child {border-bottom: none;}
#related-posts ul li a {padding: 10px;display: block;color: #222;text-decoration: none;cursor: pointer; -webkit-transition: padding-left 250ms ease-out; -moz-transition: padding-left 250ms ease-out; /* trans pattern: property duration timingMethod delay */}
#related-posts ul li a:hover {padding-left: 20px;}
<b:if cond='data:blog.pageType == &quot;item&quot;'>
    <div id='related-posts'>
        <b:loop values='data:post.labels' var='label'>
            <b:if cond='data:label.isLast != &quot;true&quot;'>
            </b:if>
            <b:if cond='data:blog.pageType == &quot;item&quot;'>
                <script expr:src='&quot;/feeds/posts/default/-/&quot; + data:label.name + &quot;?alt=json-in-script&amp;callback=related_results_labels&amp;max-results=5&quot;' type='text/javascript'/>
            </b:if>
        </b:loop>
        <script type='text/javascript'>
        var currentposturl=&quot;<data:post.url/>&quot;;
        var maxresults=5; // Increase To Show More Then 5 Related Post
        removeRelatedDuplicates();
        printRelatedLabels();
        </script>
    </div>
</b:if>

这是因为 <data:post.url/>(在你的第二个 JavaScript)应该放在 Blog1 小部件中。

尝试将以下代码放在边栏中:

<b:if cond='data:blog.pageType == &quot;item&quot;'>
    <div id='related-posts'>
        <b:loop values='data:post.labels' var='label'>
            <b:if cond='data:label.isLast != &quot;true&quot;'>
            </b:if>
            <b:if cond='data:blog.pageType == &quot;item&quot;'>
                <script expr:src='&quot;/feeds/posts/default/-/&quot; + data:label.name + &quot;?alt=json-in-script&amp;callback=related_results_labels&amp;max-results=5&quot;' type='text/javascript'/>
            </b:if>
        </b:loop>
    </div>
</b:if>

并且此代码位于 post 的 页脚(在 <data:post.body/> 下方):

<script type='text/javascript'>
var currentposturl=&quot;<data:post.url/>&quot;;
var maxresults=5; // Increase To Show More Then 5 Related Post
removeRelatedDuplicates();
printRelatedLabels();
</script>

如果此方法仍然无效,也许您需要使用另一个相关的 post 小部件,它可以放在 Blog1 小部件之外(有点难找)或附加 innerHTML 使用 JS 将你的相关 post 添加到侧边栏。