Skip to content

Instantly share code, notes, and snippets.

@astockwell
Last active July 27, 2016 17:21
Show Gist options
  • Save astockwell/d6a66dd7645d08c0360b to your computer and use it in GitHub Desktop.
Save astockwell/d6a66dd7645d08c0360b to your computer and use it in GitHub Desktop.
Teaching PHP: Basic principles to use PHP in Wordpress (with ACF)
<?php
/*
* Discovering variables:
*/
echo "string";
print_r(expression);
/*
* Flow Control:
*/
if (1 == 2) {
echo "this will never get shown";
} else {
echo "this will always be shown since the condition is false"
}
/*
* These are identical:
*/
if (condition) {
# code...
}
if (condition):
# code...
endif;
/*
* Traversing Objects/Arrays to get values:
*
* Objects: use the arrow "$the_object->the_key" !!!
* Arrays: use brackets "$the_array['the_key']" !!!
*
*/
print_r( $wp_query->query['post_type'] );
echo $wp_query->query['post_type'];
/*
* Accessing Advanced Custom Fields' field value
* THESE THREE ARE IDENTICAL!!!
*/
// Number 1:
the_field('community_name');
// Number 2:
echo get_field('community_name');
// Number 3:
$community_name = get_field('community_name');
echo $community_name;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment