How to add author details in the post sidebar?

global $authordata;

This variable contains the current post’s author data.

From an author data widget I have written:

global $authordata;
if ( ( is_singular() or is_author() )
    and is_object( $authordata )
    and isset ( $authordata->ID )
)
{
    return $authordata->ID;
}

To get the link to the author archive:

get_author_posts_url( $authordata->ID );

Update

Here is a stripped down version of my own author data widget. Very basic, adjust it to your needs. Don’t forget I18n. 😉

<?php # -*- coding: utf-8 -*-
declare ( encoding = 'UTF-8' );
/**
 * Plugin Name: T5 Author Data
 * Plugin URI:  http://wordpress.stackexchange.com/q/47418/73
 * Description: Simple Widget to display author data on singular pages (posts and pages) and author archives.
 * Version:     2012.03.31
 * Author:      Thomas Scholz <[email protected]>
 * Author URI:  http://toscho.de
 * License:     MIT
 * License URI: http://www.opensource.org/licenses/mit-license.php
 */

add_action( 'widgets_init', array ( 'T5_Author_Data', 'register' ) );

/**
 * Basic Author Data Widget.
 *
 * @author Thoams Scholz http://toscho.de
 */
class T5_Author_Data extends WP_Widget
{
    /**
     * Constructor. Calls the parent constructor
     */
    public function __construct()
    {
        parent::__construct( 't5_author_data', 'Author Data' );
    }

    /**
     * Display the content on front.
     *
     * @see WP_Widget::widget()
     */
    public function widget( $args, $instance )
    {
        // Set up $before_title, $after_title, $before_widget and $after_widget.
        extract( $args );

        global $authordata;
        if ( ! ( is_author() or is_singular() ) // wrong page
            or ! is_object( $authordata )       // wrong type
            or ! isset ( $authordata->ID )      // wrong object
            )
        {
            return; // Nothing to do.
        }

        // Build the title.
        $name = esc_html( $authordata->display_name );

        // Important! Never omit this filter in your widgets.
        $name = apply_filters( 'widget_title', $name, $instance, $this->id_base );

        // Link to archive.
        ! is_author() and $name = sprintf(
            '<a href="https://wordpress.stackexchange.com/questions/47418/%1$s" rel="author">%2$s</a>',
            get_author_posts_url( $authordata->ID ),
            $name
        );
        $title = $before_title . $name . $after_title;

        // Description.
        // See http://wordpress.stackexchange.com/a/43742/73 for a rich text
        // editor option as an alternative.
        $desc = trim( get_the_author_meta( 'description', $authordata->ID ) );
        '' !== $desc and $desc="<div class="author-text">" . wpautop( $desc ) . '</div>';

        // Image
        $img = get_avatar( $authordata->ID, $size = 32 );

        // Now we have everything we need.
        print $before_widget
            . $title
            . "<div class="author-image">$img</div>"
            . "<div class="author-text">$desc</div>"
            . $after_widget
        ;
    }

    /**
     * Handler for backend wp-admin/widget.php.
     *
     * @see WP_Widget::form()
     */
    public function form( $instance )
    {
        printf(
            '<p class="no-options-widget">%s</p>',
            __( 'There are no options for this widget.' )
        );
    }

    /**
     * Register this widget.
     *
     * @wp-hook widgets_init
     */
    public static function register()
    {
        register_widget( __CLASS__ );
    }
}

Leave a Comment