最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

auto link word link in content

programmeradmin1浏览0评论

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 0
Add a comment  | 

1 Answer 1

Reset to default 0

If 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论