Skip to content

Instantly share code, notes, and snippets.

@joelstransky
Last active September 2, 2016 02:14
Show Gist options
  • Save joelstransky/5c0f3086f4599b6d5ad10877c990c791 to your computer and use it in GitHub Desktop.
Save joelstransky/5c0f3086f4599b6d5ad10877c990c791 to your computer and use it in GitHub Desktop.
PHP: scope of global keyword
<?php
/**
* Template Name: My Page Template
*/
?>
<?php // the_post() already uses `global $post;`, so why is it needed again on line 11 in content-my_page.php? ?>
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part('templates/page', 'header'); ?>
<?php get_template_part('templates/content', 'my_page'); ?>
<?php endwhile; ?>
<div class="row">
<div class="content-main">
<?php
// check if the flexible content field has rows of data
if( have_rows('flexible_blocks') ):
// loop through the rows of data
while ( have_rows('flexible_blocks') ) : $row = the_row();
switch (get_row_layout()) {
case 'inserted_content':
global $post; // <-- Why is this required in order the following call to post_class() to work?
$post = get_post( get_sub_field('inserted_item') );
setup_postdata( $post );
?>
<article <?php post_class(); ?>>
<div class="aggregate-main">
<?php vpa_render_post_object( $post ); ?>
</div>
<?php if ( embedded_has_supplemental($row) ) : ?>
<aside class="sidebar">
<?php acf_the_supplemental( $post->ID ); ?>
</aside>
<?php endif; ?>
</article>
<?php
break;
default:
# code...
break;
}
endwhile;
reset_rows();
else :
// no layouts found
endif;
?>
</div>
</div> <!-- .row -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment