2013-03-16 51 views
2

與WordPress,稱site_url()返回完整的網站URL(http://www.example.com) 我試圖做的是增加在與過濾網址的結尾東西(add-something-here)。編輯SITE_URL與過濾

我期待的結果是: http://www.example.com/add-something-here

是否有人知道該怎麼做與過濾器?

我嘗試沒有成功如下:

function custom_site_url($url) { 
    return get_site_url('/add-something-here'); 
} 
add_filter('site_url', 'custom_site_url'); 

回答

0

未經檢驗的,但因爲它是PHP可能你只是嘗試...

function custom_site_url($url) { 
    return get_site_url() . '/add-something-here'; 
} 
add_filter('site_url', 'custom_site_url'); 
+0

不工作,我得到一個沒有錯誤的空白頁 – Fredmat 2013-03-16 22:56:30

2

的問題是,你是生產這種循環過濾。函數get_site_url正是調用過濾器site_url的地方。

您需要:

add_filter('site_url', 'custom_site_url'); 

function custom_site_url($url) 
{ 
    if(is_admin()) // you probably don't want this in admin side 
     return $url; 

    return $url .'/something'; 
} 

請記住,這可能會產生錯誤,對於依賴於真實的URL腳本。

0

未經檢驗的,但這是什麼,我經常使用的一個片段:

$constant = 'add-something-here'; 
return '<a href="'. esc_url(site_url().'/'.$constant.'/'">'. esc_html($name).'</a>'; 

,或者你可能只是想獲得頁面ID而不是和它的要好得多。

$page_id = '2014'; 
return <a href="'. get_page_link ($page_id) .'">'.esc_html__('pagename', 'text-domain').'</a>