Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8505997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:28:27+00:00 2026-06-11T02:28:27+00:00

I am using ajax to load a new post in wordpress. Here is the

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:28:29+00:00Added an answer on June 11, 2026 at 2:28 am

    You could use a template, but you’d need to write your own and it’d get a little fiddly. Andrew M.’s comment about a jQuery solution is correct, as are you: you would still be downloading the entire document, but you could only insert the part you want quite easily. See the section on loading page fragments for more details, but for the sake of expedience:

    $('#result').load('ajax/test.html #container');
    

    would load test.html and insert the contents of that document’s #container element into the #result element on the parent page. Easy as pie.

    This will, of course, result in as much server load and cost as much bandwidth as fully rendering the source page, though fortunately images and such will only be loaded if they’re part of the rendered section. This overhead shouldn’t be anything to worry about unless you have a massive amount of traffic, however.

    Let’s say that you want the server to just send the data you need in the first place: how you’d go about this depends on what other needs you have of the WordPress instance.

    If you need to load just one page, and you don’t have humans looking at the original page directly, then it’s simple – write a template that consists only of:

    while ( have_posts() ) : the_post();
      echo '<h2>';
      the_title();
      echo '</h2>';
      the_content();
    endwhile;
    

    and set that template for the post in question.

    But if you need people to look at the post on the source page as well, you’re going to have to create a theme with a singles template as described above as well as install a theme switcher plugin, and pass the necessary mojo to invoke your machine-readable theme in your AJAX request. Which is a little complicated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to load an html document using ajax and then perform a
I am using the jquery ajax api so load in some content, the code
I am using jQuery to load a page by AJAX using $.ajax (suppose test.html).
EDIT: iam using ajax to load text in my content that is why onload
I'm going to build an app which is using AJAX to load resources as
So I have a system using jQuery AJAX to load the content dynamically. I
I have a page where I load a table of information using ajax. Once
I'm loading part of my webpage using AJAX, in particular jQuery.load() . With this
I am using jquery-ui tabs and ajax to load the content of the tabs.
I am using TinyMCE load via AJAX but on the same domain, same protocol

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.