We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question, Blog

Getting block content into a json feed

Hey,

Could you offer some direction as to how I would go about getting out blocks into a json feed? Here are the blocks;

<perch:blocks>
    <perch:block type="image" label="Image">
        <perch:template path="content/blocks-blog/image.html" rescope="parent" />
    </perch:block>

    <perch:block type="image_full" label="Image Full">
        <perch:template path="content/blocks-blog/image_full.html" rescope="parent" />
    </perch:block>

    <perch:block type="download" label="Download">
        <perch:template path="content/file_pdf.html" rescope="parent" />
    </perch:block>

    <perch:block type="text" label="Text">
        <perch:template path="content/text_block.html" rescope="parent" />
    </perch:block>

    <perch:block type="quote" label="Quote">
        <perch:template path="content/block_quote.html" rescope="parent" />
    </perch:block>

    <perch:block type="video" label="Video">
        <perch:template path="content/videos/video.html" rescope="parent" />
    </perch:block>
</perch:blocks>
~~~~

Here is my code for the feed so far;

<?php include($_SERVER['DOCUMENT_ROOT'].'/cms/runtime.php');

$feed = [
    'items'         => [],
];

$items =    perch_blog_custom(array(
    'sort'              => 'postDateTime',
    'sort-order'        =>  'DESC',
    'count'             => 9999,
    'skip-template'     => true,
    'category'  => array('news'),
));

if (count($items)) {
foreach($items as $item) {
    $feed['items'][] = [
        'id'             => $item['postID'],
        'date_published' => date('c', strtotime($item['postDateTime'])),
        'title'          => $item['postTitle'],
        'slug'           => $item['postSlug'],
        'excerpt'        => $item['excerpt'],
        'content_html'   => $item['postDescHTML'],
        'content_image'  => $item['image'],
        'quote'          => $item['block.quote'],
    ];
}

}

header('Content-Type: application/json'); echo json_encode($feed); ~~~

Terry Upton

Terry Upton 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you'll find the blocks output in $item['_blocks']