0

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

2
  • Kresimir, do you need an answer regarding how to put the ads into a fixed spot or do you need help incorporating that functionality into the Query Loop Block? Can you include the code you're working with as that may help get an answer.CommentedJun 28, 2024 at 19:36
  • Dear @TonyDjukic I need to put ads into fixed position - that is what client need - and that is what pretty much what every web news portal or page has.. ads mixed with articles. I have code from previous classic theme and I'll update my question so that will mybe helpCommentedJun 29, 2024 at 19:56

1 Answer 1

1
+100

If you want to exclude output filters (which would be the normal way to do this) you are asking for a query that retrieves 9 posts and 3 ads (a separate post type or taxonomy?) in a specific order. The query block has limited querying abilities, but those can be extended to the full possibilities of WP_Query with the query_loop_block_query_vars filter. So, let's look into WP_Query.

With WP_Query you can ask for posts from different post types (or taxonomies), so you could retrieve, for instance, the 12 most recent posts and ads. But you could not specify that you want 9 of the one and 3 from the other. This is because WP_Query itself is a simplified querying system on the database. You can make more complex queries, but you need the wpdb class for that. Also, there is no obvious way to put the results in the right order.

In theory, you could use the filter to retrieve the 9+3 posts with wpdb, put the results in the right order (for instance by storing the order in a metafield, which you then use for the orderby argument), store them in a temporary post type and then make a query for the first twelve posts in that temporary type. In that way you won't need to post process the results of the query block, but it doesn't look like a very appealing option.

3
  • Dear @cjbj - thank you for detail answer - what you wrote is like I thought - except I can't use output filter because ads are out WP system that I need to inject into grid - and that didnt fly for me. Your answer pretty much assured me that with FSE, blocks this is not, and will never be doable - but classic theme with template directly, thanks!CommentedJun 29, 2024 at 19:50
  • as long as core WP goal is for higher level of abstraction - they will always fall short in cases like this .. although I secretly want some blocks ninja to come forward who has a query loop solution.. (I'll share my render code - that not only is uncool - but is crazy)CommentedJun 29, 2024 at 20:34
  • I agree. Your demand is so specific that it is difficult to cover in a system that strives to make things easy for the most common uses. The query block offers a subset of wp_query, which in turn offers a subset of wpdb. A lot of possible queries are lost along the way.
    – cjbj
    CommentedJun 30, 2024 at 12:32

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.