【问题标题】:Change placeholder text input form更改占位符文本输入表单
【发布时间】:2022-01-06 14:31:17
【问题描述】:

我正在尝试更改搜索表单输入的占位符文本。

    <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Waar ben je naar op zoek?', 'placeholder', 'fancy-lab' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />

字符串 'Waar ben je naar op zoek?' 需要在用户登录时更改为更个人化的内容。我们已经将这些更改添加到网站的其他部分,我我正在将此代码用于其他部分:

<?php global $current_user; wp_get_current_user(); ?>
    <?php if ( is_user_logged_in() ) { 
        echo $current_user->user_login; 
    }
    ?>

当我添加这个时,我得到了多个错误,第一个是由于全局部分。所以当把代码改成这样的时候:

<input type="search" class="search-field" placeholder="<?php echo esc_attr_x( global $current_user; wp_get_current_user();
                                if ( is_user_logged_in() ) { 
                                    echo $current_user->user_login; 
                                }
                                , 'placeholder', 'fancy-lab' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />

我收到错误:语法错误,意外的“全局”(T_GLOBAL)

谁能帮助我?尝试正确理解语法!

【问题讨论】:

  • “我收到多个错误” - 请分享所有错误(以及导致错误的代码及其上下文)。如果我们不知道自己在帮助什么,就很难提供帮助。
  • 我明白了,用代码和错误更新了原始问题。

标签: php input woocommerce


【解决方案1】:

在你的functions.php中创建1个方法,它将返回登录的用户用户名或字符串并在占位符中使用它:

function searchFormPlaceholder(){
    if (is_user_logged_in()) {
        $current_user = wp_get_current_user();
        echo $current_user->display_name;
    } else {
        echo 'Not logged';
    }
}

然后在占位符标签内调用你的函数:

<input type="search" class="search-field" placeholder="<?php searchFormPlaceholder(); ?>" value="<?php echo get_search_query(); ?>" name="s" />

【讨论】:

  • 谢谢!成功了!
猜你喜欢
  • 2020-02-23
  • 2017-01-01
  • 2019-02-25
  • 1970-01-01
  • 1970-01-01
  • 2020-05-12
  • 1970-01-01
  • 2018-02-09
  • 2011-06-01
相关资源
最近更新 更多