Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to add author details in the sidebar instead of bottom of the post? Can anyone tell me how to do that?

As of now i'm using like this in the loop

<?php $curauthor = get_the_author_meta('ID');  echo get_avatar($curauthor, $size = '128'); ?>

<?php the_author_posts_link(); ?>

Can anyone tell me how to do this outside loop? Thanks

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted
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 <info@toscho.de>
 * 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="%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__ );
    }
}
share|improve this answer
    
Thanks.. I want this widget only for my single.php file as a static sidebar widget. So can i use your code manually in my sidebar-single.php? If yes what are the functions i should remove? –  Giri Mar 30 '12 at 20:58
    
@Giri I have added an example plugin to show how it can be done. –  toscho Mar 31 '12 at 0:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.