简体   繁体   中英

PHP: append span tag within a href anchor text

I'm trying but I can't obtain which I need. I'm validating my html to rich snippets, and I have this code:

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

So I need to append a span tag , like:

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

I'm in wordpress, and I have:

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

where $course_category contains the a tag above.

Any help is really appreciated. Thanks, Daniel

EDIT1: I've tried by using:

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

but I don't obtain what I need

I can suggest to you to make your own html generation loop: Using get_the_terms you can get all terms and then with 1 simple loop ( foreach ) you can generate your output. I think it will be something as

  $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>';
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM