Lo que hoy les mostraré será un script con el cual podrás poner un boton o un enlace a un post random en tu blog. Es muy fácil aplicarlo.
Primero nos vamosa nuestra Plantilla y buscamos </head>, justo antes colocaremos este codigo:
<script type="text/javascript">
//<![CDATA[
var _yourBlogUrl = "http://trucosblogg.blogspot.com";
function randomPost() {
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
var theUrl = _yourBlogUrl +"/feeds/posts/summary?alt=json-in-script&callback=getTotalPostsCallback&start-index=
1&max-results=1";
script.setAttribute("src", theUrl);
document.documentElement.firstChild.appendChild(script);
};
function getTotalPostsCallback(json) {
var totalResults = json.feed.openSearch$totalResults.$t;
if (totalResults > 0) {
getRandomPostNumber(totalResults);
}
};
function getRandomPostNumber(totalResults) {
var randomNumber = Math.floor((Math.random() * totalResults) + 1);
getRandomUrl(randomNumber);
};
function getRandomUrl(randomNumber) {
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
var theUrl = _yourBlogUrl +"/feeds/posts/summary?alt=json-in-script&callback=getTheUrlCallback&start-index=" + randomNumber + "&max-results=1";
script.setAttribute("src", theUrl);
document.documentElement.firstChild.appendChild(script);
};
function getTheUrlCallback(json) {
var theUrl = json.feed.entry[0].link[0].href;
window.location.href = theUrl;
}
//]]>
</script>
Ahí solo cambiaremos la url que dice http://trucosblogg.blogspot.com por la de su blog.Despues de eso nos iremos a Pantilla agregar Nuevo Elemento HTML / JAVASCRIPT, y agregamos este otro código:
<a href="javascript:randomPost();">Ver post al azar</a>
En este codigo pueden cambiar la frase "Ver post al azar" por cualquiera, incluso podemos usar un pequeño icono o imagen, es lo mismo.
2 comentarios:
mmm perdon pero no me sirvio en mi blog http://win07noticias.blogspot.com/ :(
no funciona, tienes que editar los salto de linea para que funcione, yo lo rehice nuevamente pero a mi manera y funciono mejor ^^
Publicar un comentario