global $authordata;
此变量包含当前帖子的作者数据。
从我编写的作者数据小部件中:
global $authordata;
if ( ( is_singular() or is_author() )
and is_object( $authordata )
and isset ( $authordata->ID )
)
{
return $authordata->ID;
}
要获取作者存档的链接,请执行以下操作:
get_author_posts_url( $authordata->ID );
更新这里是我自己的作者数据小部件的精简版本。非常基本,根据您的需要进行调整。别忘了我
<?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="%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__ );
}
}