2015-04-06 60 views
1

我在嘗試但我無法獲得我需要的。 我驗證我的HTML豐富網頁摘要,我有這樣的代碼:PHP:在href錨文本中追加span標籤

<a href="http://www.xxxx.xxx/xxxxxxx" rel="tag">MY_ANCHOR_TEXT</a> 

所以我需要追加的跨度標籤,如:

<a href="http://www.xxxx.xxx/xxxxxxx" rel="tag"><span itemprop="title">MY_ANCHOR_TEXT</span></a> 

我在WordPress的是,我有:

$course_category = get_the_term_list($post->ID, 'course-cat', '', '', ''); 

其中$ course_category包含上面的標籤。

任何幫助真的很感激。 謝謝,丹尼爾

EDIT1:我已經嘗試使用:

$course_category = preg_replace('/<a\shref=\".*\">(.+<\/a>)/', '<a><span itemprop="title">$1</span>', $course_category); 

,但我沒有獲得我所需要的

+0

你知道preg_match的PHP嗎? ;) – 2015-04-06 11:36:57

+0

請發佈您迄今爲止已經嘗試爲您的網站完成這項工作? – 2015-04-06 11:37:03

+0

我試圖 $ course_category = preg_replace函數( '/(.+ <\/a>)/', '<跨度itemprop = 「標題」> $ 1',$ course_category) ; – 2015-04-06 11:37:59

回答

2

我可以建議你做你自己的HTML生成的循環: 使用get_the_terms你可以得到所有的條件,然後用1個簡單的循環(foreach)就可以生成你的輸出。 我認爲這將是

$slug = 'course-cat'; 
    $terms = get_the_terms($post->ID, $slug); 

    if (!empty($terms)) { 
    foreach ($terms as $term) { 
     echo '<a href="' . get_term_link($term->slug, $slug) .'" rel="tag"><span itemprop="title">' . $term->name . '</span></a>'; 
    } 
    } 
+0

哇!精彩!我要去試試:) – 2015-04-06 11:46:13

+0

是的,它可以工作親愛的。非常感謝你。祝你有美好的一天!!! – 2015-04-06 11:51:10