skip to Main Content

I’m trying to display the author id on the author page by using a shortcode I created but it’s currently not working because the shortcode is only outputting the administrator id.
This is what I’m returning in the shortcode:

return get_the_author_meta('ID'); 

2

Answers


  1. Working code:

    function author_id_shortcode(){
        ob_start();
        
        // We get the author ID outside loop.
        global $post;
        $author_id = $post->post_author;
        
        //  Now get the author ID inside loop.
        $author_id = get_the_author_meta( 'ID' );
        
        $output = get_the_author_meta( 'ID', $author_id );
    
        ob_end_clean();
        return $output;
        
    }
    add_shortcode( 'author_id', 'author_id_shortcode' );
    

    call the shortcode with [author_id]

    Login or Signup to reply.
  2. Please try the below code on author.php

    if (is_author()){
        $author = get_queried_object();
        $author_id = $author->ID;
        echo $author_id;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search