简体   繁体   English

PHP:在href锚文本内附加span标签

[英]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: 我正在验证我的html到丰富网页摘要,并且我有以下代码:

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

So I need to append a span tag , like: 所以我需要附加一个span标签 ,例如:

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

I'm in wordpress, and I have: 我在wordpress中,并且有:

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

where $course_category contains the a tag above. 其中$ course_category包含上面的标记。

Any help is really appreciated. 任何帮助都非常感谢。 Thanks, Daniel 谢谢,丹尼尔

EDIT1: I've tried by using: EDIT1:我尝试通过使用:

$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. 我可以建议您创建自己的html生成循环:使用get_the_terms可以获取所有条件,然后使用1个简单的循环(foreach)可以生成输出。 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>';
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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