2015-05-29 83 views
1

我創建一個wordpress主題,並使用get_archives_link列出所有存檔信息。嘗試從get_archives_link剝離li標籤

get_archives_link作爲默認顯示歸檔列表纏繞<li></li>標籤如下。

<li><a href='http://localhost/blog/?m=201505'>May 2015</a></li> 

但我想接着顯示爲一個href,而不是像下面的例子:

<a href='http://localhost/blog/?m=201505' class="list-group-item">May 2015</a> 

我如何能解決這個得到任何想法?

謝謝!

+1

是'li's和'a's的只有存在於字符串中的元素?如果是這樣的話http://php.net/manual/en/function.strip-tags.php就足夠了。 'strip_tags($ input,'');' – chris85

回答

1

嘗試設置get_archives_link()接受的第三個參數爲'blank'。例如:

get_archives_link($url, $text, 'blank', '', ''); 

注:從技術上講,要完成這項工作,第三PARAM可以是任何你喜歡接受htmllinkoption。我僅以空白爲例。

編號:https://codex.wordpress.org/Function_Reference/get_archives_link

+0

這幾乎可行。我現在只需要在href中添加一個類。我該如何解決這個問題? – Shafiq

+1

要添加類屬性,您需要使用[preg_replace()](http://php.net/manual/en/function.preg-replace.php)。 kamal pal的答案就是一個例子。 – henrywright

+0

很好...明白了。謝謝! – Shafiq

2

您可以使用preg_replace來實現這一點,見下面的例子: -

$anchorWithLi = "<li><a href='http://localhost/blog/?m=201505'>May 2015</a></li>"; 
echo preg_replace('#<li>(<a)(.*)</li>#i', '$1 class="list-group-item" $2', $anchorWithLi); 

輸出:

<a class="list-group-item" href='http://localhost/blog/?m=201505'>May 2015</a>