How to get current author's ID

(Rob) #1

Hi, just wanted to know what’s a good way to get the current authors ID?
Lets say I go to an authors page (author.php), while viewing his page I would like to pull his ID.

I tried:
<?php echo do_shortcode("[starbox id=" . get_the_author_meta('ID') . "]"); ?>

But no luck :frowning:

(Leland Fiegel) #2

Does it work when you just use <?php echo get_the_author_meta( 'ID' ); ?> in the loop?

What about when you put [starbox id=" . get_the_author_meta('ID') . "] in the content of one of your posts?

All do_shortcode does is execute whatever is inside as if it were in a post, and you typically can’t run PHP functions through them like you have done.

Does the Startbox plugin make an actual template tag available that can be used?

(Jeffrey Carandang) #3

You’ll probably just need this one :

global $post;

If get_the_author_meta isn’t working there’s probably an issue with the loop or global $authordata; based on the code here :

I hope this helps :wink:



Check out the instructions in setting up author information in the author template.

That will answers your question, but I think your problem is actually already solved. If you adding the shortcode to author.php, then [starbox] works on its own.

So drop this in your author template:

<?php echo do_shortcode('[starbox]'); ?>

To test, I copied archive.php in TwentySeventeen to author.php, so it would load sooner in the template lookup. I added the do_shortcode just below <main>:

	<div id="primary" class="content-area">
		<main id="main" class="site-main" role="main">

		<?php echo do_shortcode('[starbox]'); ?>

And it produces:

Note, if you deactivate or remove Starbox, you will print the shortcode there. :slight_smile:

(Rob) #5

@maiki thanks for the info. I actually originally tried dropping in the [starbox] shortcode and it didn’t come up on the front end like your example. That’s also how I thought it would work :frowning:

@phpbits and @leland thanks for the info. After reading the article from @phpbits I tried a couple of things. Here’s the one that worked:

<?php $AuthorID = $curauth->ID; ?>
<?php echo do_shortcode('[starbox id=' . $AuthorID . ']'); ?>     

I’m not good with php and not sure why my first solution didn’t work but this seems to be working out ok. Think I found my solution :slight_smile:

(Leland Fiegel) #6

Cool, glad you got it working. I think I may have been wrong about how do_shortcode works with other PHP stuff inside btw. :slight_smile:

(Jeffrey Carandang) #7

Seems great! Really glad to be of help :slight_smile: