2017-08-05 115 views
1

我在我的博客中使用通知栏,其中包含最新帖子的链接。问题是我每次发布内容时都必须手动更新它。有没有什么方法可以让博主在每次发布任何内容时自动设置该链接的网址?如何在博客中获取最新帖子的网址

+0

使用RSS,你会得到你博客的最新帖子 –

+0

问题依然存在,如何? –

回答

1

我们可以通过使用feed和JavaScript来实现这一点。代码将如下所示 -

<script> 
function recentPostLink(posts){ 
var link = posts.feed.entry[0].link[4].href; 
document.querySelector('.recent').href = link; // The class present on anchor tag 
} 
</script> 
<script src='https://yourblogname.blogspot.com/feeds/posts/summary?alt=json&amp;max-results=1&amp;callback=recentPostLink' /> 
+1

这不行吗? 'document.querySelectorAll('。post a')[0] .href;' –

+0

它没有工作 –

+0

@AryanAnkitRaj您可以分享博客网址,您正在测试此代码吗? –

0

Blogger拥有精选的发布部件。前往布局>添加小工具>精选文章。你可以用它来展示最新帖子通过使如果你想使用JavaScript和JSON饲料使用最新发布后

,把下一个代码的链接元素

<script> 
//<![CDATA[ 
    function recent(json) { 
     var element = document.querySelector('.link'); 
     element.innerHTML = json.feed.entry[0].title.$t; 
     element.href = json.feed.entry[0].link[4].href; 
    } 
//]]> 
</script> 
<script src='/feeds/posts/default/?alt=json-in-script&amp;callback=recent&amp;max-results=1'/> 
+0

但问题是,我不需要任何插件,我需要的是任何脚本或任何博客功能,将生成我最新的帖子的链接 –

+0

因此,请尝试我的答案更新。 – Bassam

+0

不工作! :( –

0

使用这个脚本后,到

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script><div id="recent-posts"><script language="JavaScript"> 
home_page = "Enter your blog url here"; 
numposts = 5; 

imgr = new Array();showRandomImg = false;boxwidth = 255;cellspacing = 6;borderColor = "#232c35";bgTD = "#000000";thumbwidth = 0;thumbheight = 0;fntsize = 15;acolor = "#666";aBold = true;icon = " ";text = false;showPostDate = false;summaryPost = 0;summaryFontsize = 10;summaryColor = "#666";icon2 = " ";</script><script src="http://24work-new.googlecode.com/svn/trunk/24work-blogspot/recent-posts-t/recent-posts-min-1-ycode.js" type="text/javascript"/></script><link rel="stylesheet" type="text/css" href="http://24work-new.googlecode.com/svn/trunk/24work-blogspot/recent-posts-t/recent-posts-min-3.css" /></div> 

它看起来像这样

enter image description here

相关问题