如何创建带有链接的WordPress作者下拉列表

时间:2017-03-21 作者:gdeleon101

我正在尝试创建WordPress作者的下拉列表,并提供指向其作者页面的链接。

这是我当前的代码:

<?php wp_dropdown_users(array(\'who\'=>\'authors\')); ?>
我的档案有一个类似的下拉列表,它在下面(和works)。

<select name="archive-dropdown" id="archives-dropdown--1" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr( __( \'Select Month\' ) ); ?></option> 
<?php wp_get_archives( array( \'type\' => \'monthly\', \'format\' => \'option\',\'show_post_count\' => 1 ) ); ?>
</select>
现在,作者只是在下拉列表下显示,但他们没有链接到自己的页面。我尝试将归档代码改编为我的作者代码,但没有任何效果。以下是我正在尝试的,但它不起作用:

<select name="author-dropdown" id="author-dropdown--1" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr( __( \'Select Author\' ) ); ?></option> 
<?php wp_dropdown_users(array(\'who\'=>\'authors\')); ?>
</select>               
我在WordPress编码方面还很初级。任何洞察都会很棒!如果我的情况得到解决,我希望我能帮助其他人。

谢谢

2 个回复
最合适的回答,由SO网友:Sam 整理而成

我可能错了,因为我不知道Wordpress是如何控制下拉菜单的,但看起来它可能只是返回作者的名字,而不是他们页面的链接。

我已经在Wordpress站点环境中测试了以下代码,并完成了链接到作者页面的工作。

   <select name="author-dropdown" id="author-dropdown--1" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr( __( \'Select Author\' ) ); ?></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 auther you could use another like nice_name
      echo $user->display_name;
      echo \'</option>\'; 
    } 

}
?>
</select> 

working

添加了更新以确保作者拥有帖子,然后添加到管理员不确定如何在不进行决斗的情况下组合这两个角色

<select name="author-dropdown" id="author-dropdown--1" onchange="document.location.href=this.options[this.selectedIndex].value;">
    <option value=""><?php echo esc_attr( __( \'Select Author\' ) ); ?></option> 
    <?php 
    // loop through the users
    $users = get_users(\'role=author\');
    foreach ($users as $user) 
    {
        // get user who have posts only
        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 auther you could use another like nice_name
          echo $user->display_name;
          echo \'</option>\'; 
        } 

    }

$users = get_users(\'role=administrator\');
    foreach ($users as $user) 
    {
        // get user who have posts only
        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 auther you could use another like nice_name
          echo $user->display_name;
          echo \'</option>\'; 
        } 

    }
    ?>
    </select> 

SO网友:Nathan Johnson

一种更“WordPress方式”是过滤wp_dropdown_users() 函数,而不是基本上重新创建函数。

在主题或插件中,您希望在调用之前立即添加过滤器wp_dropdown_users() 之后立即移除,以防止不必要的副作用。

add_filter( \'wp_dropdown_users\', \'wpse_260893_dropdown_users\' );
wp_dropdown_users( [ \'who\' => \'authors\' ] );
remove_filter( \'wp_dropdown_users\', \'wpse_260893_dropdown_users\' );
然后您可以将其放入插件或主题函数中。php。

//* Filter the output of wp_dropdown_users() function to replace user id with posts url
function wpse_260893_dropdown_users( $content ) {

  //* Find all cases where the option value is anything - (.*)
  $number_of_matches = preg_match_all( \'/<option value=\\\'(.*)\\\'/\', $content, $matches );

  //* If there\'s no matches, return early
  if( false === $number_of_matches || 0 === $number_of_matches ){
    return $content;
  }

  //* Get the author posts url for each of the matches found with preg_match_all()
  $posts_urls = array_map( function( $user_id ){
    return get_author_posts_url( $user_id );
  }, $matches[1] );

  //* Replace the author ids with their corresponding author posts url
  return str_replace( $matches[1], $posts_urls, $content );
}
确保在要更改wp_dropdown_users() 功能,并在您不再需要它后立即将其删除,以便它不会影响其他任何内容。

相关推荐

Can you set a role as author?

我是WordPress的新手,我正在尝试找出如何最好地设置权限。我想限制人员组只编辑某些页面(IT部门只编辑IT页面,人力资源部门只编辑人力资源页面等等)。据我所知,您取消了角色“编辑其他页面”的权限,然后将页面的作者设置为您要编辑的人。是否有一种设置多个作者的标准方法,或者是否有一种使角色成为作者而不是单个用户的方法?谢谢