Passing variables to templates (alternatives to globalizing variables)
You could also use this function: set_query_var(‘foo’, $foo); WP will extract and expose all query variables in every template it loads, so you will be able to access it as $foo
You could also use this function: set_query_var(‘foo’, $foo); WP will extract and expose all query variables in every template it loads, so you will be able to access it as $foo
You can turn your snippet into a function that returns the post thumbnail URL of a post: function wpse81577_get_small_thumb_url( $post_id ) { $thumbSmall = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), ‘small’ ); return $thumbSmall[‘0’]; } Usage, supplying the ID of a post: <?php echo wpse81577_get_small_thumb_url( 59 ); ?>
Put in the following line at the top of archive page : global $post; Thing is you need to access the global $post object before trying to use it.
Update with a better example header( ‘Content-Type: text/plain;charset=utf-8’ ); error_reporting( E_ALL | E_STRICT ); function failed_unset() { // Copy the variable to the local namespace. global $foo; // Change the value. $foo = 2; // Remove the variable. unset ( $foo ); } function successful_unset() { // Remove the global variable unset ( $GLOBALS[‘foo’] ); … Read more
There is no difference when you are using just echo. What works different is unset(): function test_unset_1() { global $post; unset( $post ); } function test_unset_2() { unset( $GLOBALS[‘post’] ); } test_unset_1(); echo $GLOBALS[‘post’]->ID; // will work test_unset_2(); echo $GLOBALS[‘post’]->ID; // will fail The reason is that unset() destroys just the local reference in the … Read more
Within the WP::parse_request() method (src) we locate the query_vars filter: $this->public_query_vars = apply_filters( ‘query_vars’, $this->public_query_vars ); and within WP::register_globals() we can see why it becomes globally accessible (src): // Extract updated query vars back into global namespace. foreach ( (array) $wp_query->query_vars as $key => $value ) { $GLOBALS[ $key ] = $value; } where the … Read more
If you are using class which needs an instance of, you have to create that instance somewhere. Currently WP has no designated convention for something like that. Common practice is to use static methods, so your hook becomes add_action(‘wp_ajax_my_action’, array(__CLASS__, ‘my_action_callback’));.
Global $post var is set by WP::register_globals() method. It is called by WP::main() method, on its turn called by wp() function that is called when wp-blog-header.php is loaded. If you look at the graph @Rarst built, on the left, you can see where wp() function is called. In terms of hooks, global post variable is … Read more
You’ll also have to fill the variable, e.g. function userinfo_global() { global $users_info; $users_info = wp_get_current_user(); } add_action( ‘init’, ‘userinfo_global’ ); And you should then be able to use $users_info everywhere in global context. Keep in mind that some template pars (header.php, footer.php, those used via get_template_part) are not in global scope by default, so … Read more
In the tutorial (Example 1), he has to declare the global $post so that he can access the post_parent from it. In a function like that, the $post is not a global variable unless he makes it so. In the codex (Example 2), it is declared global because the sample code is just a sample, … Read more