Archived topic
How to apply the generateblocks_do_content Filter?
4 replies · Started by Marvin on June 27, 2021
Hey guys!
Every project I love the GeneratePress Theme more and more. Thank you really much!
I have a custom post type heroes for a slider, which is embedded via the shortcode [heroes]:
function heroesSlider() {
ob_start();
# get posts
$args = array(
'numberposts' => -1,
'post_type' => 'heroes',
'post_status' => 'publish',
);
$heroes = get_posts($args);
# loop the slides
if(!empty($heroes)):?>
<div class="glide">
<div class="glide__track" data-glide-el="track">
<ul class="glide__slides">
<?php foreach ($heroes as $p): ?>
<?php if(has_blocks($p)): ?>
<li class="glide__slide"><?= $p->post_content ?></li>
<?php endif ?>
<?php endforeach ?>
</ul>
</div>
</div>
<script>
new Glide('.glide').mount()
</script>
<?php
endif;
return ob_get_clean();
}
add_shortcode('heroes', 'heroesSlider');
But I don't know how to apply the generateblocks_do_content on <?= $p->post_content ?> inside the loop. As expected the output looks like this, unfortunately:
<li class="glide__slide"><!-- wp:generateblocks/container {"uniqueId":"69d8e582","isDynamic":true} -->
<!-- wp:generateblocks/grid {"uniqueId":"088fad56","columns":2,"isDynamic":true} -->
<!-- wp:generateblocks/container {"uniqueId":"336f2c74","isGrid":true,"gridId":"088fad56","paddingTop":"0","paddingRight":"0","paddingBottom":"0","paddingLeft":"0","isDynamic":true} -->
<!-- wp:paragraph -->
...
</li>
How to I apply the generateblocks_do_content in a simple way on <?= $p->post_content ?>?
Thank you really much!
Marvin
Hi there,
Is there any particular reason why it has to be added through a filter?
If I may suggest:
There's a shortcode block within the Block editor which you can place your shortcode in and make it run inside a GB container block. You can try using that instead of doing a shortcode.
Hey Elvin,
thank you for your response and your smart idea!
After a break I noticed a bigger problem: I didn't parsed the blocks in the code below. Sorry for that! :( I have changed the related PHP code to this:
<?php foreach ($heroes as $post): ?>
<?php if(has_blocks($post)): ?>
<li class="glide__slide">
<?php
foreach($blocks = parse_blocks($post->post_content) as $block):
echo render_block($block);
endforeach;
?>
</li>
<?php endif ?>
<?php endforeach ?>
Now the html source code seems to be correct, but the css classes from GeneratePress are still missing. I have regenerated the css files and cleared my cache. Unfortunately embedding the shortcode via the Shortcode block not shows an effect.
Hello again,
this solution works:
function heroesSlider() {
# get posts
$args = array(
'numberposts' => -1,
'post_type' => 'heroes',
'post_status' => 'publish',
'orderby' => 'rand'
);
$heroes = get_posts($args);
ob_start();
# loop the slides
if(!empty($heroes)):?>
<div class="glide">
<div class="glide__track" data-glide-el="track">
<ul class="glide__slides" style="margin: 0">
<?php foreach ($heroes as $post): ?>
<?php if(has_blocks($post)): ?>
<li class="glide__slide">
<?php
foreach($blocks = parse_blocks($post->post_content) as $block):
$block = render_block($block);
echo $block;
endforeach;
?>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
</div>
</div>
<script>
new Glide('.glide').mount()
</script>
<?php
endif;
return ob_get_clean();
}
add_shortcode('heroes', 'heroesSlider');
/* add css classes */
function doHeroesCSS($content) {
global $post;
# check if shortcode is used
if(is_a($post, 'WP_Post') && has_shortcode($post->post_content, 'heroes')):
$args = array(
'numberposts' => -1,
'post_type' => 'heroes',
'post_status' => 'publish',
);
$heroes = get_posts($args);
if(!empty($heroes)):
foreach ($heroes as $p):
if(has_blocks($p)):
$content .= $p->post_content;
endif;
endforeach;
endif;
endif;
return $content;
};
add_filter('generateblocks_do_content', 'doHeroesCSS', 10, 2);
But if there is more efficient solution instead of looping the content two times I am interested :)
The code is pretty straightforward. A shortcode w/ a query and a filter that appends the shortcode to the content.
It looks fine. :D
Is it fully sorted out for you? Let us know.