Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

Pyrate.Ned on "Author dropdown with link to author page functionality like wp_get_archives"

$
0
0

Well I looked for a while for a dropdown of authors with the same functionality as wp_get_archives. Which makes a dropdown with a javascipt onchange which brings your to the archive you pick. After searching and reading around on google for an hour or so and looking at a few plugins. I realized I couldn't find what I was looking for so I was going to have to make the code or hack up some code. So here is what I did, I started with the functionality of wp_dropdown_users() and more then less just hacked it up to do what wp_get_archive dropdown_box does. Just copy this function into your themes functions.php

function wp_get_author( $args = '' ) {
	$authorURL = "YOUR-WORDPRESS-URL?author=";
	$defaults = array(
		'show_option_all' => '', 'show_option_none' => '', 'hide_if_only_one_author' => '',
		'orderby' => 'display_name', 'order' => 'ASC',
		'include' => '', 'exclude' => '', 'multi' => 0,
		'show' => 'display_name', 'echo' => 1,
		'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '',
		'blog_id' => $GLOBALS['blog_id'], 'who' => '', 'include_selected' => false
	);

	$defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0;

	$r = wp_parse_args( $args, $defaults );
	extract( $r, EXTR_SKIP );

	$query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who' ) );
	$query_args['fields'] = array( 'ID', 'user_login', $show );
	$users = get_users( $query_args );

	$output = '';
	if ( !empty($users) && ( empty($hide_if_only_one_author) || count($users) > 1 ) ) {
		$name = esc_attr( $name );
		if ( $multi && ! $id )
			$id = '';
		else
			$id = $id ? " id='" . esc_attr( $id ) . "'" : " id='$name'";

		$output = "<select name='{$name}'{$id} class='$class' onchange='document.location.href=this.options[this.selectedIndex].value;'>\n";
		if ( $show_option_all )
			$output .= "\t<option value='0'>$show_option_all</option>\n";

		if ( $show_option_none ) {
			$_selected = selected( -1, $selected, false );
			$output .= "\t<option value='-1'$_selected>$show_option_none</option>\n";
		}

		$found_selected = false;
		foreach ( (array) $users as $user ) {
			$user->ID = (int) $user->ID;
			$_selected = selected( $user->ID, $selected, false );
			if ( $_selected )
				$found_selected = true;
			$display = !empty($user->$show) ? $user->$show : '('. $user->user_login . ')';
			$output .= "<option value=\"\"></option>";
			$output .= "\t<option value='$authorURL$user->ID'$_selected>" . esc_html($display) . "</option>\n";
		}

		if ( $include_selected && ! $found_selected && ( $selected > 0 ) ) {
			$user = get_userdata( $selected );
			$_selected = selected( $user->ID, $selected, false );
			$display = !empty($user->$show) ? $user->$show : '('. $user->user_login . ')';
			$output .= "\t<option value='$authorURL$user->ID'$_selected>" . esc_html($display) . "</option>\n";
		}

		$output .= "</select>";
	}

	$output = apply_filters('wp_get_author', $output);

	if ( $echo )
		echo $output;

	return $output;
}

You need to edit this line and near the begging of the code.
$authorURL = "YOUR-WORDPRESS-URL?author=";

and you can also edit or remove this line
$output .= "<option value=\"\"></option>";
if you want the default option to be something like Pick an Author you would would change that link of code to be
$output .= "<option value=\"\">Pick an Author</option>";
or if you wanted it to just like the first author then you can just remove that link altogether.

Then in your template just add
wp_get_author(array('name' => 'author'));

I hope this code will help some people who are looking for a author dropdown list with links to the authors pages with a list of there posts. Please leave a comment if this code helped you =)


Viewing all articles
Browse latest Browse all 8245

Trending Articles