我今天在学习的过程中学到了很多东西,所以希望这个问题是明确的。我有下面的代码,它显示了所有的帖子由登录的用户写,只要该帖子是一个特定的类别。我想要更改的部分是URL。目标是单击链接,它将用户带到搜索页面,使用帖子名称作为搜索词。例如,如果帖子的名称是"iPhone X",那么该网址将把用户带到"iPhone X“的搜索页面,并显示所有结果。
我接近了,但问题是is...as写的,生成的URL是.../?s=iPhone-x (注意连字符)
搜索结果中的每个空格都带有一个"+“.../?s=iPhone+X
有没有一种方法可以在代码中重新构造URL,或者在生成URL之前以某种方式告诉系统将所有"-“替换为"+”?
function user_posts_exercise( ) {
if (!is_user_logged_in()) return;
$items='';
$args = array(
'post_type' => 'post',
'author' => get_current_user_id(),
'status' => 'publish',
'posts_per_page' => 10,
'category_name' => 'Exercise'
);
$jobs = get_posts( $args );
foreach($jobs as $job){
$link = '<a href="'.home_url('/?s='.$job->post_name).'">'.$job->post_title.'</a>
</BR>';
$items = $items . $link;
}
return $items;
}
add_shortcode( 'exercise_user_posts', 'user_posts_exercise' ); 发布于 2020-04-03 04:56:39
浏览器处理空格应该没有问题,但是您可以使用urlencode来转义字符。
urlencode( $job->post_title )要将空格转换为+字符以进行正确的编码,或使用word press功能
add_query_arg( 's', $job->post_title, home_url() )并让它处理编码
https://stackoverflow.com/questions/60983911
复制相似问题