WordPress.org

Ready to get started?Download WordPress

Forums

Make a json page for Ajax (5 posts)

  1. Beou
    Member
    Posted 4 years ago #

    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.

  2. dyerrington
    Member
    Posted 4 years ago #

    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?

  3. Beou
    Member
    Posted 4 years ago #

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

  4. dyerrington
    Member
    Posted 4 years ago #

    DUH right!? Thanks Beou!

    People actually reply to forum posts..

  5. dyerrington
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic