Php Wordpress用链接替换内容中的标记词
我有一个代码片段,它用链接替换了内容()中的特定单词:Php Wordpress用链接替换内容中的标记词,php,wordpress,Php,Wordpress,我有一个代码片段,它用链接替换了内容()中的特定单词: function link_words( $text ) { $replace = array( 'google' => '<a href="http://www.google.com">google</a>', 'computer' => '<a href="http://www.computer.com">computer</a>', 'keyboard' => '&l
function link_words( $text ) {
$replace = array(
'google' => '<a href="http://www.google.com">google</a>',
'computer' => '<a href="http://www.computer.com">computer</a>',
'keyboard' => '<a href="http://www.keyboard.com">keyboard</a>'
);
$text = str_replace( array_keys($replace), $replace, $text );
return $text;
}
add_filter( 'the_content', 'link_words' );
函数链接\u单词($text){
$replace=数组(
“谷歌”=>“,
'计算机'=>'',
“键盘”=>“
);
$text=str_replace(数组_键($replace),$replace,$text);
返回$text;
}
添加过滤器(“内容”、“链接词”);
我想使用获取标签()作为$replace数组,这样它就可以用指向标签存档的链接替换特定的标签词。获取标签()将返回一个WP\u Term
对象数组。您必须在这些对象之间循环以构建$replace
数组
例如:
$replace = array();
$tags = get_the_tags();
if ( $tags ) {
foreach ( $tags as $tag ) {
$replace[ $tag->name ] = sprintf( '<a href="%s">%s</a>', esc_url( get_term_link( $tag ) ), $tag->name );
}
}
$replace=array();
$tags=获取_标签();
如果($tags){
foreach($tags作为$tag){
$replace[$tag->name]=sprintf(“”,esc\u url(get\u term\u link($tag)),$tag->name);
}
}
获取标签()
将返回一个WP\u Term
对象数组。您必须在这些对象之间循环以构建$replace
数组
例如:
$replace = array();
$tags = get_the_tags();
if ( $tags ) {
foreach ( $tags as $tag ) {
$replace[ $tag->name ] = sprintf( '<a href="%s">%s</a>', esc_url( get_term_link( $tag ) ), $tag->name );
}
}
$replace=array();
$tags=获取_标签();
如果($tags){
foreach($tags作为$tag){
$replace[$tag->name]=sprintf(“”,esc\u url(get\u term\u link($tag)),$tag->name);
}
}
以下是完整的解决方案
function link_words( $text ) {
$replace = array();
$tags = get_tags();
if ( $tags ) {
foreach ( $tags as $tag ) {
$replace[ $tag->name ] = sprintf( '<a href="%s">%s</a>', esc_url( get_term_link( $tag ) ), esc_html( $tag->name ) );
}
}
$text = str_replace( array_keys($replace), $replace, $text );
return $text;
}
add_filter( 'the_content', 'link_words' );
函数链接\u单词($text){
$replace=array();
$tags=get_tags();
如果($tags){
foreach($tags作为$tag){
$replace[$tag->name]=sprintf(“”,esc_url(get_term_link($tag)),esc_html($tag->name));
}
}
$text=str_replace(数组_键($replace),$replace,$text);
返回$text;
}
添加过滤器(“内容”、“链接词”);
请注意,我没有使用get_the_tags函数,因为它只返回分配给帖子的标签,所以我使用了get_tags函数,这是完整的解决方案
function link_words( $text ) {
$replace = array();
$tags = get_tags();
if ( $tags ) {
foreach ( $tags as $tag ) {
$replace[ $tag->name ] = sprintf( '<a href="%s">%s</a>', esc_url( get_term_link( $tag ) ), esc_html( $tag->name ) );
}
}
$text = str_replace( array_keys($replace), $replace, $text );
return $text;
}
add_filter( 'the_content', 'link_words' );
函数链接\u单词($text){
$replace=array();
$tags=get_tags();
如果($tags){
foreach($tags作为$tag){
$replace[$tag->name]=sprintf(“”,esc_url(get_term_link($tag)),esc_html($tag->name));
}
}
$text=str_replace(数组_键($replace),$replace,$text);
返回$text;
}
添加过滤器(“内容”、“链接词”);
请注意,我没有使用get_the_tags函数,因为它只返回分配给帖子的标签,所以我使用了get_tags函数。问题不清楚。举个例子问题不清楚。举个例子