admin管理员组文章数量:1404923
My English is not good. sorry.
I want to link to titles of child-categories that are in the single post content. (Wordpress)
Example:
My Categories:
Aplle , Asia , Europ , North america , ...
My Post Content:
An apple is a sweet, edible fruit produced by an apple tree (Malus domestica). Apple trees are cultivated worldwide and are the most widely grown species in the genus Malus. The tree originated in Central Asia, where its wild ancestor, Malus sieversii, is still found today. Apples have been grown for thousands of years in Asia and Europe and were brought to North America by European colonists. Apples have religious and mythological significance in many cultures, including Norse, Greek and European Christian tradition.
and, My troubled PHP script for automatic create Link to category titles if it is in the single post content:
<?php
$term_id = 1209;
$taxonomy_name = 'category';
$termchildren = get_term_children( $term_id, $taxonomy_name );
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
$mycatone = "'" . $term->name . "',";
$mycattwo = '"' . "<a href='This-Category-URL-LINK'>" . $termd->name . "</a>" . '",';
}
function link_words($content){
$words = array( $mycatone );
$links = array( $mycattwo );
$content = str_replace($words , $links ,$content);
return $content;
}
add_filter('the_content', 'link_words');
?>
<?php the_content() ?>
UPDATE:
This code is work for last child-category and dont work for all child-categories of cat-id=1209:
<?php
function link_words($content){
$term_id = 1209;
$taxonomy_name = 'category';
$termchildren = get_term_children( $term_id, $taxonomy_name );
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
$termlink = get_term_link( $child, $taxonomy_name );
$mycatname = "$term->name";
$mycatlink = "<a href=$termlink> $term->name </a>";
$content = str_replace(array($mycatname), array($mycatlink) ,$content);
return $content;}
}
add_filter('the_content', 'link_words');
?>
<?php the_content() ?>
本文标签: phpHow to Link to category titles if it is in the single post content
版权声明:本文标题:php - How to Link to category titles if it is in the single post content? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744877174a2629987.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论