i use this code for auto link word in content
<?php function link_words($content){
$words=array(
'test1',
'test2',
'test3'
);
$links=array(
'<a href="/tag/test1/" rel="nofollow">test1</a>',
'<a href="/tag/test2/" rel="nofollow">test2</a>',
'<a href="/tag/test3/" rel="nofollow">test3</a>'
);
$content = str_replace($words,$links,$content);return $content;}
add_filter('the_content','link_words');
add_filter('the_excerpt','link_words'); ?>
but upper code have one problem, so changed and linked any word (images alt , ...)
i want only word between <p></p>
i want finally link any word in content to tags and categories
i use this code for auto link word in content
<?php function link_words($content){
$words=array(
'test1',
'test2',
'test3'
);
$links=array(
'<a href="/tag/test1/" rel="nofollow">test1</a>',
'<a href="/tag/test2/" rel="nofollow">test2</a>',
'<a href="/tag/test3/" rel="nofollow">test3</a>'
);
$content = str_replace($words,$links,$content);return $content;}
add_filter('the_content','link_words');
add_filter('the_excerpt','link_words'); ?>
but upper code have one problem, so changed and linked any word (images alt , ...)
i want only word between <p></p>
i want finally link any word in content to tags and categories
Share Improve this question edited Sep 21, 2019 at 13:05 user168547 asked Sep 21, 2019 at 12:46 user168547user168547 52 bronze badges 01 Answer
Reset to default 0If I understand correctly ... first of all you need to match everything inside paragraphs using regex.
$content = "<p>some text which includes test1 and test2 etc</p>";
preg_match_all("/<\s*p[^>]*>([^<]*)<\s*\/\s*p\s*>/", $content);
then you can use your code.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745140622a4613413.html
评论列表(0条)