/featured-pages-block.php Secret
Created
December 11, 2018 16:54
Editor block with wpautop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Block Name: Featured Pages | |
* | |
* This is the template that displays the featured pages block. | |
*/ | |
// Create id attribute for specific styling. | |
$id = 'featured-pages-' . $block['id']; | |
// Create align class ("alignwide") from block setting ("wide"). | |
$align_class = $block['align'] ? 'align' . $block['align'] : ''; | |
$class_list = sprintf( '%1$s %2$s', $align_class, $block['className'] ); | |
printf( '<div id="%1$s" class="wp-block-candid-featured-pages %2$s">', esc_attr( $id ), esc_attr( $class_list ) ); | |
printf( '<%1$s>%2$s</%1$s>', | |
wp_kses( get_field( 'heading_tag' ), [ | |
'h1' => [], | |
'h2' => [], | |
'h3' => [], | |
'h4' => [], | |
'h5' => [], | |
'h6' => [], | |
'p' => [], | |
'span' => [], | |
'div' => [], | |
] ), | |
wp_kses_post( get_field( 'section_heading' ) ) | |
); | |
$field = acf_get_local_field( 'featured_posts' ); | |
$featured_posts = get_field( 'featured_posts' ); | |
$query_args = [ | |
'post_type' => $field['post_type'], // Defined inside the ACF field. | |
'post__in' => $featured_posts, | |
'orderby' => 'post__in', | |
]; | |
$featured_posts_query = new WP_Query( $query_args ); | |
?> | |
<?php | |
if ( $featured_posts_query->have_posts() ) : | |
while ( $featured_posts_query->have_posts() ) : | |
$featured_posts_query->the_post(); | |
ob_start(); | |
?> | |
<section class="featured-page" id="page-<?php the_ID(); ?>">' | |
<figure> | |
<?php | |
if ( has_post_thumbnail() ) : | |
the_post_thumbnail( 'featured-pages' ); | |
else : | |
// Use a blank gif placeholder to fill space. | |
?> | |
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" style="width: 225px; height: 225px;" alt=""> | |
<?php endif; ?> | |
<figcaption> | |
<h3> | |
<a href="<?php the_permalink(); ?>"> | |
<?php the_title(); ?> | |
</a> | |
</h3> | |
<?php the_excerpt(); ?> | |
</figcaption> | |
</figure> | |
<a href="<?php the_permalink(); ?>" class="read-more"><span class="screen-reader-text">Get more info about <?php the_title(); ?></span>›</a> | |
</section> | |
<?php | |
$markup = ob_get_clean(); | |
$markup = str_replace( [ "\n", "\t" ], '', $markup ); | |
echo wp_kses_post( $markup ); | |
?> | |
<?php endwhile; ?> | |
<?php $featured_posts_query->reset_postdata(); ?> | |
<?php else : ?> | |
<div class="notice">No Posts Found</div> | |
<?php endif; ?> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment