简体   繁体   中英

str_replace is replacing string on wrong place

I am redesigning template mechanism of my one of the wordpress plugin. So the users can customized design as per their need. For this purpose. i am asking a template format string with some options which i can replace later.

Sample Template String:

$template = '<a href="%POST_URL%"  title="%POST_TITLE%"><img src="%POST_THUMB%" /></a>';

Code:

$temp .= $template;
$temp = str_replace("%POST_TITLE%", $ptitle, $temp);
$temp = str_replace("%POST_URL%", the_permalink(), $temp);
$temp = str_replace("%POST_THUMB%", lead_img_thumb_post($width ,$height ,$imagepath ,$icontype ), $temp);

Output:

%POST_URL%%POST_URL%%POST_URL%<a href=""  title="%POST_TITLE%"><img src="%POST_THUMB%" /></a><a href=""  title="%POST_TITLE%"><img src="%POST_THUMB%" /></a><a href=""  title="%POST_TITLE%"><img src="%POST_THUMB%" /></a>

Why %POST_URL% is moving ahead

You need to use get_the_permalink() instead. the_permalink() outputs the link to the browser direction. Using the get_whatever() instead will cause WP to return the data, instead of outputting it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM