I've got a partial called within
single.php that looks like this:
<?php $userdata = get_userdata($post->post_author) ; ?> <div class="entry-meta"> <span class="byline author vcard"><?= __('By', 'sage'); ?> <a href="<?= get_author_posts_url(get_the_author_meta('ID')); ?>" rel="author" class="fn"> <?php echo ucfirst($userdata->user_nicename) ?> </a> </span> </div>
$userdata might be useful elsewhere within my single. So I'd like the variable and its value to be available globally within all templates that are included whenever my single is used.
So I cut out the first line: the creation of
$userdata and put earlier in a 'parent' template that gets called earlier in the loop.
Alas, the variable was no longer avaiable to the partial. I tried a few other templates that are also called earlier in the loop. I got the same result: the variable wasn't available.
I thought about creating a function within
functions.php. But I can think of a couple of reasons not to do this. First of all, why bother with an abstraction for
get_userdata already exists? This seems inelegant.