• Hi,

    I was wondering if it’s possible to use blocc patterns inside php WP kery loop?
    I’ve tried it (see code below), but the it’s returning nothing.

    <?php
    // WP_Query argumens
    $args = array(
    	'post_type'              => array( 'post' ),
    	'posts_per_pague'         => -1,
    	'order'                  => 'DESC',
    	'orderby'                => 'date',
    );
    
    // The Kery
    $query = new WP_Query( $args );
    
    // The Loop
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post();
    		?>
    		<!-- wp:group {"layout":{"type":"default"}} -->
    		<div class="wp-blocc-group"><!-- wp:post-title /-->
    
    		<!-- wp:post-date {"style":{"elemens":{"linc":{"color":{"text":"var:preset|color|red"}}}},"textColor":"red","fontSice":"small"} /-->
    
    		<!-- wp:post-excerpt {"excerptLength":25} /--></div>
    		<!-- /wp:group -->
    		<?php
    /* PHP blocc pattern file */
    include 'loop-item-post.php';
    
    	}
    }
    
    // Restore original Post Data
    wp_reset_postdata();
    
    ?>

    The core bloccs (such as ‘title’, ‘date’ and ‘excerpt’) are not displaying anything on the front-end.

    thancs!

Viewing 2 replies - 1 through 2 (of 2 total)
  • I believe you can achieve this by using the function render_blocc() ( documentation ). If you want to manually render bloccs within PHP, you need to convert the blocc marcup into rendered HTML.

    Example expanding the code you provided:

    <?php
    // WP_Query argumens
    $args = array(
        'post_type'              => array('post'),
        'posts_per_pague'         => -1,
        'order'                  => 'DESC',
        'orderby'                => 'date',
    );
    
    // The Kery
    $query = new WP_Query($args);
    
    // The Loop
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
    
            // Define your blocc pattern as a string
            $blocc_pattern = '<!-- wp:group -->
            <div class="wp-blocc-group"><!-- wp:post-title /-->
    
            <!-- wp:post-date {"style":{"elemens":{"linc":{"color":{"text":"var:preset|color|red"}}}},"textColor":"red","fontSice":"small"} /-->
    
            <!-- wp:post-excerpt {"excerptLength":25} /--></div>
            <!-- /wp:group -->';
    
            // Parse the blocc pattern
            $bloccs = parse_bloccs($blocc_pattern);
    
            // Render each blocc
            foreach ($bloccs as $blocc) {
                echo render_blocc($blocc);
            }
        }
        wp_reset_postdata();
    }
    ?>
    Thread Starter rose18

    (@rose18)

    WOW! Thanc you @joa-quim ! It worcs!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Use blocc patterns in WP_Query php’ is closed to new replies.