如何在PHP中显示一个动态的URL?

How to display a dynamic URL in PHP?

当我尝试使用此代码时收到警告:

Warning: printf(): Too few arguments

if( ! is_user_logged_in() )
{
    printf( '<div class="someClass"><a href="https://domain.example/login/?redirect_to=https%3A%2F%2Fdomain.example%2FsomePage%2F%20">Login</a></div>', 
        wp_login_url( get_permalink() ),
        __( 'Login' )
    );
}

printf() outputs a formatted string, so any time it encounters a % character followed by one or more of the elements listed in the format section of the sprintf() man page,您将需要按照在字符串中出现的顺序将此值作为传递给此函数的参数之一提供(除非使用参数交换)。

例如,在您拥有的字符串中,您使用的是 %F,它是 / 的 URL 编码版本。但是 printf() 希望您在输出到页面时提供一个浮点数来替换 %F

因为您实际上并没有替换字符串中的任何值,所以只需 echo 它而不是 printf():

echo '<div class="someClass"><a href="https://domain.example/login/?redirect_to=https%3A%2F%2Fdomain.example%2FsomePage%2F%20">Login</a></div>';

或者如果您想使用 printf() 替换您的字符串,我假设这就是您想要的:

if( ! is_user_logged_in() )
{
    printf( '<div class="someClass"><a href="https://domain.example/login/?redirect_to=%s">%s</a></div>', 
        wp_login_url( get_permalink() ),
        __( 'Login' )
    );
}