I am using ajax to load a new post in wordpress. Here is the basic code:
function test(){
var menuitem = document.getElementsByTagName('nav')[0].childNodes;
for(var i= 0; i < menuitem.length; i++)
{
bindEvt(menuitem[i], "click", loadajax);
}
};
function loadajax (event) {
event.preventDefault();
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
var content = document.getElementsByTagName('article')[0];
if(xhr.readyState == 4){
if(xhr.status == 200) {
content.innerHTML = xhr.responseText;
} else{
content.innerHTML = 'Cannot connect to server. Check your internet connection'}
}
};
xhr.open('GET', this.href, true);
xhr.send();
}
bindEvt(window, "load", test);
It works fine, only it loads the entire new post with menu, header, footer, etc... I only need the content and the comments. Is there any way using ajax to specifically ask wordpress for those contents or is the only way possible to do this to get the entire page and then extract only the content I need out of it and repost it?
Maybe make a specific template page for it? but how would I go about getting that working.
I hope I have been clear. Please tell me if not! First attempt at a Wordpress Theme/PHP.
Thanks for your help!
.load()can be used with a selector: api.jquery.com/load