luni, 6 iulie 2009

Post la intamplare (random post) in Blogger

Oarecum o idee interesanta ar fi sa pui in sidebar un widget cu posturi la intamplare (random post). Ideea ar fi ca, daca dai clic pe acel link sa se afiseze un articol la intamplare din blogul tau.





Daca doriti sa faceti asta, urmati calea "Aspect --> Elemente de pagina" si dati clic pe "Adauga un obiect gadget". Din lista de gadget-uri alegeti "HTML/JavaScript".

Dati un titlu gadget-ului, de tipul Post la intamplare sau Random post, sau daca vreti puteti sa-l lasati gol. In campul pentru continut adaugati urmatorul cod:


Link simplu - afiseaza un link simplu

<div id="myLuckyPost"></div> <script type="text/javascript"> 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 feelingLucky(root){ var feed = root.feed; var total = parseInt(feed.openSearch$totalResults.$t,10); var luckyNumber = Math.floor(Math.random()*total);luckyNumber++; a = document.createElement('a'); a.href = '#random'; a.rel = luckyNumber; a.onclick = function(){fetchLuck(this.rel);}; a.innerHTML = 'Te simti norocos, smechere?'; document.getElementById('myLuckyPost').appendChild(a); } </script> <script src="/feeds/posts/summary?max-results=0&alt=json-in-script&callback=feelingLucky"></script>

Textul scris cu rosu (Te simti norocos, smechere?) este textul care va aparea in sidebar. Schimbati-l cum doriti.



Link cu poza - afiseaza o imagine cu link


<div style="text-align: center;">
<div id="myLuckyPost"></div> <script type="text/javascript"> 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 feelingLucky(root){ var feed = root.feed; var total = parseInt(feed.openSearch$totalResults.$t,10); var luckyNumber = Math.floor(Math.random()*total);luckyNumber++; a = document.createElement('a'); a.href = '#random'; a.rel = luckyNumber; a.onclick = function(){fetchLuck(this.rel);}; a.innerHTML = '<img width="128" alt="HIT ME!" src="http://4.bp.blogspot.com/_cNpXRtRph18/SXzRfSHYchI/AAAAAAAADuQ/0FBHxPFcap4/s400/random_posts_widget.png" height="128"/>'; document.getElementById('myLuckyPost').appendChild(a); } </script> <script src="/feeds/posts/summary?max-results=0&alt=json-in-script&callback=feelingLucky"></script>
</div>


Daca vreti sa schimbati poza (poza este cea din post) schimbati Url-ul pozei (cel scris cu rosu) cu Url-ul pozei voastre.

Dupa cum vedeti inaltimea si latimea pozei este setata la 128 pixeli. Daca aveti o poza mai mare sau sidebarul mai lat, modificati aceste valori cu cele dorite de voi.

Si asta e tot.

0 comentarii:

Trimiteți un comentariu