简体   繁体   English

在WordPress中的the_permalink()上使用php substr

[英]Using the php substr on the_permalink() in WordPress

My urls for posts in WordPress looks like this: http://localhost:8888/blabla/book/yes-vi-testar 我在WordPress中发布帖子的网址看起来像这样: http:// localhost:8888 / blabla / book / yes-vi-testar

Using the_permalink() would generate " http://localhost:8888/blabla/book/yes-vi-testar " but I want to cut the first 34 characters to get a string like "yes-vi-testar". 使用the_permalink()会生成“ http:// localhost:8888 / blabla / book / yes-vi-testar ”,但我想剪切前34个字符以获取类似于“ yes-vi-testar”的字符串。 How do I use php substr in a case like this? 在这种情况下,如何使用php substr? I'm confused... I tried 我很困惑...我尝试过

<?php
    $friendlypermalink = substr(the_permalink(), 34);
?>

but that doesnt do the trick. 但这并不能解决问题。

Use get_the_permalink to get the permalink without echoing it 使用get_the_permalink获得永久链接而不回显它

So 所以

substr(get_the_permalink(), .............);

A lot of of the Wordpress function have 'return' alternates using get as the operative word. 许多Wordpress函数使用get作为操作词具有“返回”替代项。 IE: get_the_time , get_the_content , etc. IE: get_the_timeget_the_content等。

the_title is the only one I believe that doesn't have this option. 我相信只有the_title没有此选项。 For the_title you have to pass two empty parameters (the before and after seperators) and either a true or false ... not sure at the moment 对于the_title您必须传递两个空参数(分隔符之前和之后),然后输入true或false ...目前不确定

the_title("","",true);

As Chacha says, use get_the_permalink(). 正如Chacha所说,请使用get_the_permalink()。 You can then do something like: 然后,您可以执行以下操作:

$url = get_the_permalink();
$text = substr($url, strrpos($url, '/') + 1);

//or

preg_match('~[^/]+$~', get_the_permalink(), $m);
$text = $m[0];

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

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