WordPress.org

Support

Support » How-To and Troubleshooting » Make a json page for Ajax

Make a json page for Ajax

  • Hi erveryone,

    Recently I tried to make a page with some info about my posts encoded in JSON. So, I create a page template with this code:


    <?php
    /*
    Template Name: Ajax loader
    */
    header("Content-Type: text/json");
    ?>
    [<?php
    global $post;
    $myposts = get_posts();
    foreach($myposts as $post){
    $id = get_the_ID();
    $title = get_the_title();
    $content = get_the_content();

    ?>{"title":<?php echo json_encode($title); ?>,"content":<?php echo json_encode($content); ?>,"id":<?php echo $id; ?>},<?php
    }
    ?>{"end":true}]

    The title appear, but not the content and the ID..

    Could someone help me ? Please ?

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Sorry I can’t help you with this but it seems you are a bit further ahead of me with a similar type of development idea.

    How do you even display JSON without a header, footer and all that other garbage? Is it a custom action or what?

    Just don’t put the get_header() or get_footer() on the page template and it will not appear.

    DUH right!? Thanks Beou!

    People actually reply to forum posts..

    Beou, since you are using $post as global, perhaps you can use $post->ID? Check it out, works for me when the other way doesn’t.

    var_dump($post) says allot too.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Make a json page for Ajax’ is closed to new replies.