I have a PHP snippet that is partially working. It’s a dropdown menu that will redirect to the author page when selected. But, if I use the same snippet on the author’s page (to view another author), the page reloads and some extra code is added to the url.
Note that my authors pages are subcategories of the category author, so the urls look like /author/john-smith
Here’s the code I’m using (patched together from various snips I found)
<select class="select-css" name="author-dropdown" id="author-dropdown--1" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr( __( 'CONTRIBUTORS' ) ); ?></option>
<?php
// loop through the users
$users = get_users('role=author');
foreach ($users as $user)
{
if(count_user_posts( $user->id ) >0)
{
// We need to add our url to the authors page
echo '<option value="'.get_author_posts_url( $user->id ).'">';
// Display name of the author you could use another like nice_name
echo $user->display_name;
echo '</option>';
}
}
?>
</select>
I’ve tried adding /author/ to the echo, but I don’t know PHP well enough to really understand how to fix this. Anyone have any ideas?`
Gino Whitaker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.