Skip to content

Instantly share code, notes, and snippets.

@mwangepatrick
Created December 15, 2021 07:50
Show Gist options
  • Save mwangepatrick/9d91350772a280673b837927510d27f7 to your computer and use it in GitHub Desktop.
Save mwangepatrick/9d91350772a280673b837927510d27f7 to your computer and use it in GitHub Desktop.
<?php
/**
* Newton Block Template.
*
* @param array $block The block settings and attributes.
* @param string $content The block inner HTML (empty).
* @param bool $is_preview True during AJAX preview.
* @param (int|string) $post_id The post ID this block is saved to.
*/
// Create id attribute allowing for custom "anchor" value.
$id = 'newton-' . $block['id'];
if( !empty($block['anchor']) ) {
$id = $block['anchor'];
}
// Create class attribute allowing for custom "className" and "align" values.
$className = 'newton';
if( !empty($block['className']) ) {
$className .= ' ' . $block['className'];
}
if( !empty($block['align']) ) {
$className .= ' align' . $block['align'];
}
// Load values and assign defaults.
$team = get_field('team') ?: 'Your team here...';
$position = get_field('position') ?: 'Teamn position here';
$country = get_field('country') ?: "Team's Country";
$flag = get_field('flag') ?: 121;
$background_color = get_field('background_color');
//do a WP query
//get_posts
//you can use a post object field ==> array of the posts, then you can loop through the posts.
//
?>
<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
<blockquote class="newton-blockquote">
<span class="newton-text">Team: <?php echo $team; ?></span>
<span class="newton-author">Position: <?php echo $position; ?></span>
<span class="newton-role">Country: <?php echo $country; ?></span>
</blockquote>
<div class="testimonial-image">
<?php echo wp_get_attachment_image( $flag, 'thumbnail' ); ?>
</div>
<style type="text/css">
#<?php echo $id; ?> {
background: <?php echo $background_color; ?>;
}
</style>
</div>
?>
<?php
//functions.php
add_action('acf/init', 'my_acf_init_block_types');
function my_acf_init_block_types() {
// Check function exists.
if( function_exists('acf_register_block_type') ) {
// register a testimonial block.
acf_register_block_type(array(
'name' => 'newton_block',
'title' => __('A Newton Block'),
'description' => __('A custom Newton block.'),
'render_template' => 'template-parts/blocks/newton/newton.php',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array( 'newton', 'patrick', 'testing', 'training' ),
));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment