【发布时间】:2018-06-22 08:02:04
【问题描述】:
我真的很沮丧,我是 wordpress 的新手,我正在尝试限制标签链接的最大数量出现在帖子内容文章中。下面是我的代码。我不知道如何解决它。
function link_words( $text ) {
$replace = array();
$tags = get_tags();
$count=0;
if ( $tags ) {
foreach ( $tags as $tag ) {
$count++;
$replace[ $tag->name ] = sprintf( '<a href="%s">%s</a>', esc_url( get_term_link( $tag ) ), esc_html( $tag->name ) );
if( $count > 2 ) break;
}
}
$text = str_replace( array_keys($replace), $replace, $text );
return $text;
}
add_filter( 'the_content', 'link_words' );
【问题讨论】:
-
您发布的代码有什么问题?它与预期输出有何不同?有没有报错?
-
它没有给出错误,但链接没有添加限制。它仍然与帖子内容中的链接相同
标签: wordpress wordpress-theming