2013-08-22 21 views
0

好吧我有一個問題,在這裏我發現很難解決。雖然我猜測它可能不那麼複雜。在html中獲取the_permanlink(),然後在functions.php中格式化

我使用WordPress的,在我的functions.php我有以下幾點:

<?php 
class EXAMPLE { 
$this->url=rawurlencode($url); 
} 

function DO-SOMETHING() { 
DO-SOMETHING . $this->url); 
} 
?> 

然後我調用該函數在我的HTML:

<div> 
<?php $obj=new EXAMPLE("www.example.com"); echo $obj->DO-SOMETHING();?> 
</div> 

我需要更換(「WWW .example.com「)與(the_permalink())。 但它不起作用。不知道爲什麼。我已經嘗試了許多使用試驗和錯誤不同的方式,但無法找到一個解決方案

感謝

+0

對於具有相同問題的人,我在這裏找到了答案 [http://stackoverflow.com/questions/18397052/get-permalink-for-json-decode-not-working/18436334#18436334] [1] [1]:http://stackoverflow.com/questions/18397052/get-permalink-for-json-decode-not-working/18436334#18436334 – user2052581

回答

0

如果它只是編碼URL,下面會做:

echo rawurlencode(get_the_permalink()); 

你也可以換,在一個函數:

echo myrawurlencode(get_the_permalink()); 

function myrawurlencode($url){ 
    rawurlencode($url); 
} 

注:get_the_permalink()返回URL,而the_permalink()呼應它directl y默認。

如果您打算使用課程,您可能首先需要查看http://php.net/manual/en/language.oop5.decon.phphttp://www.php.net/manual/en/language.oop5.basic.php

+0

試過,但不起作用。該函數格式化json返回。完整的代碼可以在這裏找到[鏈接](http://toolspot.org/script-to-get-shared-count.php)。問題是在代碼中我不能把get_the_permalink()放在引號內「」。如果我沒有他們這樣做,該網址不會被格式化的PHP。 – user2052581

0

好像提到的the_peramlink()使用echo語句顯示網址,但您可以使用get_permalink($id)返回url。 WordPress確保兩個函數返回的url都被轉義,並且可以在不使用PHP的rawurlencode()函數的情況下使用。

除非你需要追加一些東西到永久鏈接,你不需要這個函數,並且應該和get_permalink($ id)函數一樣好。您可以隨時查看Wordpress codex中的get_permalink文檔以獲取更多信息。