We all want to personalize our blogs or websites. May it be in terms of the content displayed, or the features presented. In my attempt to add a custom feature in my blog, I went about exploring how to add author information in a sidebar widget.
[space]
Using the PHP Code Widget
WordPress allows us to add text and html in a normal text widget. But the author information to be shown, is not simple text, the content is dynamic. I needed to get information related to the author of the current post and present it. To achieve this, I installed and activated the PHP Code Widget Plugin. This plugin provided me with a widget, which allowed me to add php code. The widget then handled executing the php code and displaying the results.
This plugin made my job really simple. I added the PHP Code Widget to the sidebar widgets. I then fetched and displayed the author information, using simple php code.
[pre]<?php
echo get_the_author_meta(first_name).’ ‘.get_the_author_meta(last_name).’<br/>’.get_the_author_meta(description);
?>[/pre]
Above is an example to display the author’s full name and biographical information. You can refer to the WordPress function get_the_author_meta for additional options.
[space]
How to List All Posts By the Author
In addition to author information, I wanted to display the links to other posts written by the same author. I was able to do this using the following:
[pre]$args = array( ‘author’ => get_the_author_meta(ID), ‘post__not_in’ => array( $post->ID ));
$author_other_posts = get_posts($args );
$posts_list = ‘<ul>’;
foreach ( $author_other_posts as $other_post ) {
$posts_list .= ‘<li><a href=”‘ . get_permalink( $other_post->ID ) . ‘”>’.$other_post ->post_title.'</a></li>’;
}
$posts_list.= ‘</ul>’;
echo $posts_list;[/pre]
[space]
Displaying Author Widgets only in Blog Posts
Of course it made sense to display the author widget only for blog posts and not other pages. This can be achieved by conditionally displaying the widget using the Widget Logic Plugin. The Widget Logic Plugin adds an extra field to every widget.
You can use this field to specify a condition, which will control the display of the widget. The widget will be displayed only if the condition is evaluated to true.
[space]
Conclusion
Using these two simple plugins, PHP Code Widget and Widget Logic I was successfully able to create a customized author widget. Additionally, you could use simple CSS to style your widget. Do let me know if this article helped you create your own custom widget. If you had any questions, you could let me know in the comments section below.