简体   繁体   English

替换get_permalink()函数中的字符串?

[英]replace string in get_permalink() function?

I'm struggling to rewrite URLs. 我正在努力重写URL。 BC facebook doesn't follow 301 redirects for Likes (ugh) I'm needing to find a way to rewrite the og:url parameter wordpress spits out to get http in there instead of https. BC facebook没有为Likes(ugh)遵循301重定向。我需要找到一种方法来重写og:url参数wordpress吐出,以获取http而不是https。

I'm trying the following to no avail: 我尝试以下无济于事:

<?php 
$oldlink = get_permalink(get_the_id());
$newlink = str_replace('https', 'http', $oldlink);
?>
<meta property="og:url" content="<?php echo $newlink ?>"/>

Any ideas why this method might not work? 任何想法为什么这种方法可能行不通?

If you're using it in the header.php and the page you're loading is single post/page/custom post you can use the_post(); 如果您在header.php中使用它,并且正在加载的页面是单个帖子/页面/自定义帖子,则可以使用the_post();。 before your statement in order to initiate the loop. 在您的语句之前,以启动循环。

<?php the_post(); ?>
<meta property="og:url" content="<?php $oldlink = get_permalink(get_the_id()); $newlink=str_replace('https','http',$oldlink); echo $newlink; ?>" />

The thing is that the get_the_id() function works only within the loop, so with out initiating it it returns fault results causing get_permalink function to return nothing useful. 事实是,get_the_id()函数仅在循环内起作用,因此如果不初始化它,它将返回错误结果,从而导致get_permalink函数不返回任何有用的信息。

But be careful, if you call the_post later again on the same single page after the first one it can mess up your data output. 但是请注意,如果稍后在第一个页面之后的同一页面上再次调用the_post,则可能会使您的数据输出混乱。 So be sure to call it only once per single page (if you're not creating custom loop of cource). 因此,请确保每个页面仅调用一次(如果您没有创建自定义循环)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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