2

Wordpress 提供了一个名为“the_permalink()”的函数,它在帖子循环中返回给定帖子的永久链接,您猜对了!

我正在尝试对该永久链接进行 URL 编码,并且当我执行此代码时:

<?php
print(the_permalink());
$permalink = the_permalink();
print($permalink);
print(urlencode(the_permalink()));
print(urlencode($permalink));
$url = 'http://wpmu.local/graphjam/2008/11/06/test4/';
print($url);
print(urlencode($url));
?>

它以 HTML 格式生成这些结果:

http://wpmu.local/graphjam/2008/11/06/test4/
http://wpmu.local/graphjam/2008/11/06/test4/
http://wpmu.local/graphjam/2008/11/06/test4/
http://wpmu.local/graphjam/2008/11/06/test4/
http%3A%2F%2Fwpmu.local%2Fgraphjam%2F2008%2F11%2F06%2Ftest4%2F

我希望输出的第 2、3 和 5 行是 URL 编码的,但只有第 5 行是这样。想法?

4

3 回答 3

12

根据文档,the_permalink打印永久链接并返回它。所以,urlencode没有得到任何编码。

试试get_permalink


[编辑]

编辑有点晚了,但我没有意识到打印数量是个问题。

他们都来自这里:

<?php
print(the_permalink());                                // prints (1)
$permalink = the_permalink();                          // prints (2)
print($permalink);                                     // nothing
print(urlencode(the_permalink()));                     // prints (3)
print(urlencode($permalink));                          // nothing
$url = 'http://wpmu.local/graphjam/2008/11/06/test4/'; 
print($url);                                           // prints (4)
print(urlencode($url));                                // prints (5)
?>
于 2008-11-08T01:22:47.670 回答
7

the_permalink() 呼应永久链接

get_the_permalink() 返回永久链接,以便可以将其分配给变量。

(WordPress 中的大多数函数也是如此:the_something() 有一个 get_the_something() 来返回值而不是回显它)

于 2008-11-12T09:45:22.160 回答
5

@Jonathan has the reason why, and the way you should deal with it in WordPress (ie. use the right function for the job).

Here is how to fix it when there isn't a function that returns a string:

ob_start();
the_permalink();
$permalink = ob_get_clean();
print(urlencode($permalink));
于 2008-11-08T03:27:22.987 回答