Skip to content

Instantly share code, notes, and snippets.

@mwangepatrick
Created December 20, 2021 17:03
Show Gist options
  • Save mwangepatrick/e9b28a01c586053b9e7ee1b976eee936 to your computer and use it in GitHub Desktop.
Save mwangepatrick/e9b28a01c586053b9e7ee1b976eee936 to your computer and use it in GitHub Desktop.
<?php
/**
* Testimonial 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 = 'testimonial-' . $block['id'];
if( !empty($block['anchor']) ) {
$id = $block['anchor'];
}
// Create class attribute allowing for custom "className" and "align" values.
$className = 'testimonial';
if( !empty($block['className']) ) {
$className .= ' ' . $block['className'];
}
if( !empty($block['align']) ) {
$className .= ' align' . $block['align'];
}
// Load values and assing defaults.
$text = get_field('testimonial') ?: 'Your testimonial here...';
$author = get_field('author') ?: 'Author name';
$role = get_field('role') ?: 'Author role';
$image = get_field('image') ?: 10;
if(is_int($image)){
$post_thumbnail_srcset = wp_get_attachment_image_srcset( $image, 'medium' );
$post_thumbnail = wp_get_attachment_image_src($image);
}else{
$post_thumbnail_srcset = wp_get_attachment_image_srcset( $image["ID"], 'medium' );
$post_thumbnail = $image['sizes']['thumbnail'];
}
$background_color = get_field('background_color');
$text_color = get_field('text_color');
?>
<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
<blockquote class="testimonial-blockquote">
<span class="testimonial-text"><?php echo $text; ?></span>
<span class="testimonial-author"><?php echo $author; ?></span>
<span class="testimonial-role"><?php echo $role; ?></span>
</blockquote>
<div class="testimonial-image">
<img
src="<?php echo $post_thumbnail; ?>"
srcset="<?php echo esc_attr( $post_thumbnail_srcset ); ?>"
alt="<?php echo $alt['alt'] ?>"
>
</div>
<style type="text/css">
#<?php echo $id; ?> {
background: <?php echo $background_color; ?>;
color: <?php echo $text_color; ?>;
}
</style>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment