最合适的回答,由SO网友:George G. 整理而成
你已经说过你试过了urlencode()
, 但你用什么试过?如果你试过的话urlencode()
值来自the_title()
或the_title_attibute()
到目前为止,请尝试get_the_title()
直接地如果我尝试使用一个小测试短代码,我可以通过尝试得到不同的结果:
使用the_title()
: 
function wpse_123927_cb() {
return urlencode( the_title() );
}
add_shortcode(\'test_wpse_123927\', \'wpse_123927_cb\');
使用
the_title_attribute()
:

function wpse_123927_cb() {
return urlencode( the_title_attribute() );
}
add_shortcode(\'test_wpse_123927\', \'wpse_123927_cb\');
使用
get_the_title()
:

function wpse_123927_cb() {
return urlencode( get_the_title() );
}
add_shortcode(\'test_wpse_123927\', \'wpse_123927_cb\');
第三种解决方案在这两方面都很有效。我在这里使用这个小代码来测试:
function wpse_123927_cb() {
$encode = esc_attr( urlencode( get_the_title() ) );
$decode = urldecode($encode);
return \'Encoded: \' . $encode . \'<br>Decoded: \'. $decode;
}
add_shortcode(\'test_wpse_123927\', \'wpse_123927_cb\');
并得到以下输出:

希望有帮助!