Support

Account

Home Forums Gutenberg Dynamic blocks Reply To: Dynamic blocks

  • You can manipulate $post in your block and then use the standard post loop:

    $posts = get_posts(array('post_type' => 'YOUR_POSTYPE', 'posts_per_page' => 3 ));

    and then

    if( $posts ): ?>
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
            
            // Your code
    
        <?php endforeach; ?>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>

    If you want to give the user the option to change, let’s say the ammount of posts, then you can set the value of a field as variable in your array:

    $number_of_posts = get_field('your field');
    $posts = get_posts(array('post_type' => 'YOUR_POSTYPE', 'posts_per_page' => $number_of_posts ));