-
-
Save soham2008xyz/cc0f25575de7cc738e1288a15013cf23 to your computer and use it in GitHub Desktop.
Sage Wordpress Query Loop for Blade templates
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 | |
| /** | |
| * Add this to your setup.php | |
| * Usage @loop( $wp_query ) or $loop and end with @endloop | |
| * the new $loop variable is available | |
| */ | |
| /** | |
| * Create @loop Blade directive | |
| */ | |
| sage('blade')->compiler()->directive('loop', function ( $query = null ) { | |
| global $wp_query; | |
| if(!$query) $query = $wp_query; | |
| $initLoop = "\$__currentLoopData = {$query}; \$__env->addLoop(\$__currentLoopData->posts);"; | |
| $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__currentLoopData->the_post();'; | |
| return "<?php {$initLoop} while(\$__currentLoopData->have_posts()): {$iterateLoop} ?>"; | |
| }); | |
| /** | |
| * Create @endloop Blade directive | |
| */ | |
| sage('blade')->compiler()->directive('endloop', function () { | |
| return '<?php endwhile; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>'; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment