You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress作者下拉菜单在作者页跳转异常问题求助

问题描述

我有一段部分可用的PHP代码,实现的是一个下拉菜单,选中选项后跳转至对应作者页面。但在作者页面使用该片段切换其他作者时,页面重载后URL会附加多余参数(比如?mab_v3=18258)。我的作者页面URL格式为/author/john-smith,属于author分类的子分类。

原PHP代码片段

<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>

作者页面渲染后的HTML源码

<div class="et_pb_text_inner">
    <select class="select-css" name="author-dropdown" id="author-dropdown--1" onchange="document.location.href=this.options[this.selectedIndex].value;">
        <option value="">CONTRIBUTORS</option> 
        <option value="https://active-staging.mysite.com/author/anil-daka/?mab_v3=18258">Aaron Socker</option>
        <option value="https://active-staging.mysite.com/author/anil-daka/?mab_v3=18258">Aden Gebeyehu</option>
    </select>
</div>

解决方案

方法1:清理URL中的查询参数

使用remove_query_arg函数过滤get_author_posts_url返回的地址,移除所有多余参数:

<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)
    {
        // 清理URL,移除所有查询参数
        $clean_author_url = remove_query_arg(null, get_author_posts_url( $user->id ));
        echo '<option value="'.esc_url($clean_author_url).'">';
        echo esc_html($user->display_name);
        echo '</option>'; 
    } 
}
?>
</select>

方法2:直接构造干净的作者URL

如果你的固定链接结构是/author/用户名,可以直接用用户的user_nicename构造URL,避免依赖可能被修改的函数返回值:

<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)
    {
        // 直接构造干净的作者URL
        $clean_author_url = home_url('/author/' . $user->user_nicename . '/');
        echo '<option value="'.esc_url($clean_author_url).'">';
        echo esc_html($user->display_name);
        echo '</option>'; 
    } 
}
?>
</select>

额外优化说明

  • 用esc_url过滤URL,保证输出安全;
  • 用esc_html过滤作者名称,防止XSS风险;
  • 两种方法都能确保跳转后的URL是干净的/author/用户名格式,不会附加多余参数。

内容的提问来源于stack exchange,提问作者Gino Whitaker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 16:29:58