簡體   English   中英

PHP:在href錨文本內附加span標簽

[英]PHP: append span tag within a href anchor text

我正在嘗試,但無法獲得所需的東西。 我正在驗證我的html到豐富網頁摘要,並且我有以下代碼:

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

所以我需要附加一個span標簽 ,例如:

<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 );

但我沒有得到我所需要的

我可以建議您創建自己的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>';
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM