Is the child item the same as the parent item? $ Term_id = $ child?

Source: Internet
Author: User
Is the child item the same as the parent item? $ Term_id $ child? In the get_term_children function:
The sub-item and parent item will be the same item ??? $ Term_id = $ child ???
The following is the source code, which has been confusing for a long time.
/**
* Merge all term children into a single array of their IDs.
*
* This recursive function will merge all of the children of $ term into the same
* Array of term IDs. Only useful for taxonomies which are hierarchical.
*
* Will return an empty array if $ term does not exist in $ taxonomy.
*
* @ Since 2.3.0
*
* @ Param string $ term_id of Term to get children.
* @ Param string $ taxonomy Taxonomy Name.
* @ Return array | WP_Error List of Term IDs. WP_Error returned if '$ taxonomy' does not exist.
*/
Function get_term_children ($ term_id, $ taxonomy ){
If (! Taxonomy_exists ($ taxonomy ))
Return new WP_Error ('invalid_taxonomy', _ ('invalidtaxonomy '));

$ Term_id = intval ($ term_id );

$ Terms = _ get_term_hierarchy ($ taxonomy );

If (! Isset ($ terms [$ term_id])
Return array ();

$ Children = $ terms [$ term_id];

Foreach (array) $ terms [$ term_id] as $ child ){
If ($ term_id = $ child ){
Continue;
}

If (isset ($ terms [$ child]) // if the subitem also has a subitem
$ Children = array_merge ($ children, get_term_children ($ child, $ taxonomy ));
}

Return $ children;
}
Appendix:
/**
* Retrieves children of taxonomy as Term IDs.
* Subitem only returns term_id
* @ Ignore
* @ Since 2.3.0
*
* @ Param string $ taxonomy Taxonomy name.
* @ Return array Empty if $ taxonomy isn't hierarchical or returns children as Term IDs.
*/
Function _ get_term_hierarchy ($ taxonomy ){
If (! Is_taxonomy_hierarchical ($ taxonomy ))
Return array ();
$ Children = get_option ("{$ taxonomy} _ children ");

If (is_array ($ children ))
Return $ children;
$ Children = array ();
$ Terms = get_terms ($ taxonomy, array ('get' => 'all', 'orderby' => 'id ', 'fields' => 'Id => parent '));
Foreach ($ terms as $ term_id => $ parent ){
If ($ parent> 0)
$ Children [$ parent] [] = $ term_id;
}
Update_option ("{$ taxonomy} _ children", $ children );

Return $ children;
}


Reply to discussion (solution)

The data restrictions of each column in the database table cannot be guaranteed to have practical significance.
If ($ term_id = $ child) -- verify the data validity.
This is not the case if you insert data in the wordpress background. However, it cannot be guaranteed that the data inserted directly in the database is in this situation.

Get_term_children is used to extract all subcategories.
Parameters
$ Term_id indicates the Category id for the start check.
$ Taxonomy is the category name

$ Terms = _ get_term_hierarchy ($ taxonomy );
Retrieve the Category id set to be checked by name

Foreach (array) $ terms [$ term_id] as $ child ){
If ($ term_id = $ child ){
Continue;
}
Traverse the obtained Category id set and perform corresponding operations

Since $ term_id is a category id, it is not normal for it to appear in the category id set.

Logically, you cannot be your parent. Upstairs is right. However, we retrieve data from the database. The data here is added from the website background or directly from the operation database. If ($ term_id = $ child) ---- in this way, the data inserted from the website background is one more step: it may be of practical significance to directly operate the data added to the database.
I wonder whether this understanding is comprehensive.

Because $ terms = _ get_term_hierarchy ($ taxonomy );
Is a collection of classification IDs to be checked by name.
Therefore, you cannot determine that $ term_id is not included in the result of _ get_term_hierarchy ($ taxonomy ).
Because the name and id are not unique, besides _ get_term_hierarchy or fuzzy query

The answer is in function wp_insert_term ($ term, $ taxonomy, $ args = array ----
If the taxonomy is hierarchical, and the 'parent' argument is not empty,
The term is inserted and the term_id will be given.
The parent arguments does not need to be different from the term_id.
Weak: will there be equal conditions? This may occur if the following code is used:
Foreach (array) $ terms [$ term_id] as $ child ){
If ($ term_id = $ child ){
Continue;
}
I don't know how to understand this, right? Please advise.

$ Terms = _ get_term_hierarchy ($ taxonomy );
$ Terms is obtained from other sources, so it is normal that $ term_id is contained in $ terms [$ term_id ].
He has handled it like this.
If ($ term_id = $ child) {// if you are yourself
Continue; // continue the loop without any other operations
}
So there is no need to continue to struggle.

If you just want to know: under what circumstances will there be $ term_id = $ child
You can print $ terms and $ taxonomy out before continue.



Iteration, return the item end from the bottom
In this way, there is no problem with understanding.

You cannot understand iteration (recursion) at a time. it can only confuse you.



Recursive features: function A repeatedly calls itself. function B has an exit and an initial value.

The exit you marked is not really an exit!
Continue is a process control statement.

Continue is used in the loop structure to skip the remaining code in this loop and start to execute the next loop when the condition value is true.

There is no exit for this recursion, right? I cannot find it.

Recursion is conditional: recursion is performed only when a subitem exists.

As you said, what is this? Not recursion? Confused. Please advise

Recursion
Yes: conditional access (breadth first)
Yes: conditional return (depth first)
Specific use should be determined based on the application scenario

I am not clear about the traversal and loop processes, which leads to confusion. Thank you for your advice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.