简体   繁体   中英

How can I get Wordpress user ID?

I want to use the Twitter follow button for site authors on all posts.

The Twitter structure is here:

<a href="https://twitter.com/twitterapi" class="twitter-follow-button" data-show-count="false" data-lang="en">Follow @twitterapi</a>

Yes, it works but this place must be generic. I should import Wordpress $user->ID in this structure. How can I do this?

You can use get_current_user_id() in wordpress.

To get the author of the current post try the following

  <a href="https://twitter.com/<?php esc_attr_e(get_the_author()) ?>" 
     class="twitter-follow-button" 
     data-show-count="false" data-lang="en">Follow @<?php echo get_the_author() ?></a>

To get the ID of the current user logged you can simple user this wordpress function:

get_current_user_id()

To put it in whatever html code you should just open the tags where you need a dynamic value.

<a href="https://twitter.com/twitterapi" class="twitter-follow-button" data-show-count="false" data-lang="en">Follow <?php get_current_user_id() ?></a>

or using shortcut tags:

<a href="https://twitter.com/twitterapi" class="twitter-follow-button" data-show-count="false" data-lang="en">Follow <?=get_current_user_id()?></a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM