For client I need to create a simple 3x4 article grid that needs to have ads on fixed places instead of articles
X .. X .. Y Y .. X .. X X .. X .. X X .. Y .. X
in example above Y is ad, and X is article card.
Query loop easily can build grid - but for inserting ads on fixed places I haven't found solution.
Can someone please share how to achieve this.. because I'm going mad? - or is this not doable with block editor and FSE altogether?
p.s. hooking on render_block
or other output filters doesn't work - I'd like blocks - FSE solution because that is in core now..
Thanks
p.s. as example here is a code that I created 10years+ ago - similar grid directly in template (rendered everything cool - wrapped in div for no layout issues) - and I've tried to recreate this in query-loop block - or FSE editor
https://gist.github.com/mkdizajn/4843bf99ca55f905dd3b72a2c5118ff7
Here is my code for output filter that failed miserably - because ad html is sometimes invalid (real-life usecase) - and DOMDocument can't render it then
function putadsthere( $cont, $block ) { // QUERY BLOCK ===> INSERT ADS if ( $block['blockName'] === 'core/query' ) { $dom = new DOMDocument(); $cont = mb_convert_encoding($cont, 'HTML-ENTITIES', "UTF-8"); $dom->loadHTML($cont); // find article carts $finder = new DomXPath($dom); $classname="wp-block-post"; $nodes = $finder->query("//li[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]"); // make counter and insert ads $i = 0; $tmpDoc = new DOMDocument(); $tmpDoc->loadHTML( adrotate_group(4) ); foreach ($nodes as $node) { if ( in_array( $i, [3, 5, 9] ) ) { // count posts // $node->insertBefore($tmpDoc->documentElement, TRUE); } $i++; } $html = $dom->saveHTML(); $cont = preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( ['<html>', '</html>', '<body>', '</body>'], '', $html )); } return $cont; } add_filter( 'render_block', 'putadsthere', 10, 2 );
ref: this