按钮/链接显示博客中特定标签的随机帖子

时间:2018-03-13 18:07:13

标签: javascript blogger blogspot

我需要帮助处理使用Googles博主CMS建立的博客:https://funatonce.blogspot.com

我目前正在使用以下脚本生成/显示随机帖子,此脚本会从整个帖子条目中返回随机帖子。我想要一个类似的按钮来显示特定类别/标签的帖子,甚至是混合类别/标签。

我有超过5000个帖子&我的博客上有50多个类别/标签,这就是为什么它对我的访问者来说非常有用。

<script type='text/javascript'>
//<![CDATA[
function showLucky(root){
    var feed = root.feed;
    var entries = feed.entry || [];
    var entry = feed.entry[0];
      for (var j = 0; j < entry.link.length; ++j) {
       if (entry.link[j].rel == "alternate") {
       window.location = entry.link[j].href;
       }
      }
   }

function fetchLuck(luck){
    script = document.createElement('script');
    script.src = '/feeds/posts/summary?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky';
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);
   }
function readLucky(root){
    var feed = root.feed;
    var total = parseInt(feed.openSearch$totalResults.$t,10);
    var luckyNumber = Math.floor(Math.random()*total);
    luckyNumber++;
    fetchLuck(luckyNumber);
    }
function feelingLucky(){
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = '/feeds/posts/summary?max-results=0&alt=json-in-script&callback=readLucky';
    document.getElementsByTagName('head')[0].appendChild(script);
    }
//]]>
</script>

<a href="#random" onclick="feelingLucky()" title="Random Post">Feeling Lucky?</a>

1 个答案:

答案 0 :(得分:0)

通过标签添加<script type='text/javascript'> //<![CDATA[ function showLucky(root){ var feed = root.feed; var entries = feed.entry || []; var entry = feed.entry[0]; for (var j = 0; j < entry.link.length; ++j) { if (entry.link[j].rel == "alternate") { window.location = entry.link[j].href; } } } function fetchLuck(luck){ script = document.createElement('script'); script.src = '/feeds/posts/summary/-/LabelName?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky'; script.type = 'text/javascript'; document.getElementsByTagName('head')[0].appendChild(script); } function readLucky(root){ var feed = root.feed; var total = parseInt(feed.openSearch$totalResults.$t,10); var luckyNumber = Math.floor(Math.random()*total); luckyNumber++; fetchLuck(luckyNumber); } function feelingLucky(){ var script = document.createElement('script'); script.type = 'text/javascript'; script.src = '/feeds/posts/summary/-/LabelName?max-results=0&alt=json-in-script&callback=readLucky'; document.getElementsByTagName('head')[0].appendChild(script); } //]]> </script> 到脚本src获取帖子,如下所示

{{1}}