Drupal - loop all Terms under a Taxonomy - drupal

Hi
Suppose I have taxonomy "test", and under this taxonomy, I have terms "test2" and "test3". I want to loop all terms under taxonomy "test'. I found a api function "taxonomy_get_children" that can get all children (reference: http://api.drupal.org/api/drupal/modules--taxonomy--taxonomy.module/function/taxonomy_get_children/6), my code didn't works, could someone give me a idea?(I am using Drupal 6)
Thank you
<?php
$a = taxonomy_get_children(1, $vid, 'tid');
print_r($a);
?>

Are you sure your tid = 1? Or is your $vid = 1
tid = term id (the actual term in the vocabulary)
vid = vocab id (the vocabulary where the term exists)
Typically, if you know your vocabulary id is 1, youd call it like this:
$a = taxonomy_get_children($tid, 1);
print_r($a);
Or if you don't know the vid, just pass in the $tid
$a = taxonomy_get_children($tid);
print_r($a);

Related

Subcategory before parent category in Wordpress

I have a category named Z and a subcategory named A.
In my function, I am using this to get category Z (Parent)
$category[0]->slug
The problem is that subcategory A is set as [0] because it is alphabetical order. Z is the parent category, so Z should be [0]. I don't get why it is in this way.
I recommend to use this wordpress function to do that:
//Load data for your Sub-Category
$subcategory = get_category("A");
//from Sub-Category get parent ID ( Z )
$parent_category_ID = $subcategory->parent;
//load object for Parent-Category ( Z )
$objet_parent_category = get_category($parent_category);
//Get the Parent Category slug
$parent_name = $parent_name->slug; // to get name: ->name
I hope help you!

Advanced Custom Fields data from ancestor page

I am trying to make ACF data available on child pages two levels under the parent. I have a solution for making it available to a child page:
if ( $post->post_parent ) {
$headingFont = get_field('community_heading_font', $post->post_parent);
$bodyFont = get_field('community_body_font', $post->post_parent);
$primaryColor = get_field('community_primary', $post->post_parent);
$secondaryColor = get_field('community_secondary', $post->post_parent);
$fifteenSecondaryColor = get_field('community_fifteen_secondary', $post->post_parent);
$tertiaryColor = get_field('community_tertiary', $post->post_parent);
}
However, this information isn't available once we're a level deeper. That is, the ACF field 'community_heading_font' isn't available to the grandchild of the page originally providing data for that field.
I've tried post->post_parent->post_parent, and I've also tried to use post->post_parent on a variable:
$parentPage = $post->post_parent;
$grandparentPage = $parentPage->post_parent
To get the $grandparentPage ID for use in your ACF functions, use the wp_get_post_parent_id() function.
$grandparentPage = wp_get_post_parent_id($post->post_parent);
$headingFont = get_field('community_heading_font', $grandparentPage);
https://codex.wordpress.org/Function_Reference/wp_get_post_parent_id

symfony get data from db in while loop

I need to grab data from a symfony 2 db. The data looks like :
Parent-Element [id 15]
-> Child Element [id 20, parent ID 15]
--->Child Element [id 27, parent ID 20]
----->Child Element [id 34, Parent ID 27]
....
The Childs are assigned to each parent Element by a cathegory ID see [ ].
There might be more than one Child Elements per level.
In php it would be easy to grab this by a while loop. But in Symfony im stucking. Can anyone help me finding a solution? IS there a while loop in symfony?
Kind regdards
Philipp
EDIT: I mean, in "normal" php I would do a simple while or create an array with ids which I loop through by another while loop... In smyfony, I would use a queryBuilder like that
$query = $em->createQueryBuilder()
->select('d')
->from('PrUserBundle:Location', 'd')
->where('d.client_id = :client_id')
->setParameter('client_id', $this->clientId)
->getQuery();
$results=$query->getResult();
Where I don't see any possibility to grab any other Ids or sort it so that I can render a parent-child listing.
What about iterators:
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($iterator as $key => $value) {
echo "$key => $value\n";
}

How to query the custom fields by language in wordpress?

For each post, there is a custom field name "Function", the key/value pair is like this:
Key : Functions
Value : <!--en-->Nourishing Yin and invigorating the vital essence of kidneys.<!--:--><!--tw-->滋陰補腎。<!--:-->
The problem is if I simply use get_post_meta , it return string of both language, how can I get the value based on the language?
I am using qTranslate right now, Thanks.
Updated (the code):
$custom_fields = get_post_custom(get_the_ID());
$function = get_post_custom_values('Functions', get_the_ID());
You can simply fetch the strings considering comments as prefix and suffix -
After you get the custom field value,
e.g.
$function = "<!--en-->Nourishing Yin and invigorating the vital essence of kidneys.<!--:--><!--tw-->滋陰補腎。<!--:-->";
$arr = explode("<!--:-->", $function);
$new_arr = array();
foreach($arr as $a ){
if(!empty($a)){
$lang = str_replace( "-->", "", substr($a, 4, 5) );
$str = substr($a, 9);
$new_arr[$lang] = $str;
}
}
Now $new_arr will have key/value pairs like array(language_code => sentence).
If you do print_r($new_arr);
It will give output as follows:
Array ( [en] => Nourishing Yin and invigorating the vital essence of kidneys. [tw] => 滋陰補腎。 )
Now you can identify the strings using their respective language codes.

How to insert term into vocabulary named Tag

I am trying to populate nodes by writing a script...i am using following code
$node->field_tags['und'][0]['textfield'] = 'first_tag';
$node->field_tags['und'][0]['textarea'] = 'This is tag';
$node->field_tags['und'][0]['tid'] = 2;
node_save($node);
whatsits doing is nothing and the term is not being added to vocabulary named tag...if i write the following code it will select the already existed term call xml..i guess because of tid=1;
$node->field_tags['und'][0]['textfield'] = 'first_tag';
$node->field_tags['und'][0]['textarea'] = 'This is tag';
$node->field_tags['und'][0]['tid'] = 2;
node_save($node);
i even tried the following code
$node->field_tags['und'][0]['name'] = 'first_tag';
$node->field_tags['und'][0]['description'] = 'This is tag';
$node->field_tags['und'][0]['tid'] = 2;
node_save($node);
can someone please help me how add terms into a already existed vocabulary
Get dynamically vid using below function
$vid = taxonomy_vocabulary_machine_name_load('vocabname');
$terms = new stdClass();
$terms->vid = $vid;
$terms->name='whatever name of your term';
taxonomy_term_save($terms);
Thanks,
Ankush
got it after reading hundreds of line code
$terms = new stdClass();
$terms->vid=1;
$terms->name='success';
$get_tid=new stdClass();
taxonomy_term_save($terms);
term will be save in vocabulary vid=1 with automatic tid....

Resources