2015-05-29 81 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>