How to Link to category titles if it is in the single post content?

Question

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() ?>
0
Ramin 2 years 2019-12-24T09:40:24-05:00 0 Answers 67 views 0

Leave an answer

Browse
Browse