Created
April 22, 2019 22:58
-
-
Save lukecarbis/031096956421e0fa3b8389dc16eb6b6a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/** | |
* Custom block to create a gallery based on image folder in uploads. | |
* | |
* @package Block Lab | |
*/ | |
?> | |
<div class="singlepost__media__image has-pswp"> | |
<?php | |
$home_url = home_url() . '/wp-content/uploads' . block_value( 'imagefolder' ); | |
$upload_dir = wp_upload_dir(); | |
$image_folder = $upload_dir['basedir'] . block_value( 'imagefolder' ); | |
$files = glob( $image_folder . '/*.jpg' ); | |
foreach ( $files as $image_name ) { | |
if ( ! $files ) { | |
continue; | |
} | |
$file = $home_url . basename( $image_name ); | |
?> | |
<figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"> | |
<a href="<?php echo esc_attr( $file ); ?>" itemprop="contentUrl" class="singlepost__media__image__link"> | |
<img class="lazy" data-object-fit="cover" alt="<?php echo esc_attr( $file ); ?>" src="<?php echo esc_attr( $file ); ?>"> | |
</a> | |
</figure> | |
<?php | |
} | |
?> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a response to this support ticket: https://wordpress.org/support/topic/block-preview-not-working/
A few notes on this approach:
$home_url
was set tohome_url() . '/website/wp-content/uploads'
. I'm not sure whywebsite
would be a part of that URL, since if WordPress was installed in a subdirectory, that should still be included in the result ofhome_url()
<div>
is output whether there are any results or not. It's important that we return something.