It's rendering taxonmy terms. I need only published terms. There are two languages in site. Publish status can be translated. I need help with condition. I don't work with php. Could anyone help?
foreach ($child_terms as $term) {
$taxonomy_term = \Drupal\taxonomy\Entity\Term::load($term->tid);
$taxonomy_term_trans = \Drupal::service('entity.repository')->getTranslationFromContext($taxonomy_term, $curr_langcode);
if (????) {
$child_term_options[$term->tid] = $taxonomy_term_trans->getName();
}
}
Assuming you pretend to loop terms that are children of some given term.
Assuming you know the term id of the parent term.
<?php
$status = TRUE;
$lang_code = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
$child_terms = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadByProperties([
'parent' => $parent_tid,
'status' => $status,
'langcode' => $lang_code,
]);
foreach ($child_terms as $term) {
$name = $term->getName();
$tid = $term->id();
}
Related
I want to list all pages as well as selected pages in A-Z listing in WordPress. I know there are a number of plugins available, but I want this without a plugin.
Update
sorry if question is not clear, i want A-Z listing like attached image
i have solved this issue,
just need to put if condition, here is the code
$arr[0] = array(2=>2983);
$arr[1] = array(2=>2981);
$arr[2] = array('A'=>20);
$arr[3] = array('A'=>25);
print "<pre>";
print_r($arr);
$newArry = array();
foreach ($arr as $a) {
foreach ($a as $key => $value) {
if (array_key_exists($key, $newArry)) {
//$newArry[$key] = array($value);
array_push($newArry[$key], $value);
} else {
$newArry[$key] = array($value);
}
echo "<br/> Key ".$key ." => Value ".$value;
//print_r($b);
}
}
print_r($newArry);
You can use a WP_Query, as page is simply a post type.
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
);
$query = new WP_Query( $args );
$pages = $query->posts;
See WP_Query documentation.
How can i get the no of posts inside a term of a custom taxonomy ? (including the posts that are attached to a child term).
For example i have:
term (2 posts)
-child term (2 posts)
--child child term (1 post)
Right now i'm, doing it like this:
$categories = get_terms($taxonomy,$args);
foreach ($categories as $categ) {
print $categ->name.' / '.$categ->count;
}
But for "term" i get only 2 posts when i really need to show 5( 2 from "term" and 3 from it's children).
Thanks
There is an easier way to do this: do a standard WP_Query with taxonomy parameters:
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'your_custom_taxonomy',
'field' => 'slug',
'terms' => 'your-parent-term-name',
),
),
);
$query = new WP_Query( $args );
// Get the count
$count = $query->post_count;
If you don't know the name(s) of terms within a taxonomy, you can do a query and pass their IDs as an array to WP_Query. See this post on WPSE for more info.
This is something I run into few times, and I changed the code from PatchRanger on bountify to make it work with taxonomies (so please note this solution is based on his work):
function wp_get_term_postcount($term) {
$count = (int) $term->count;
$tax_terms = get_terms($term->taxonomy, array('child_of' => $term->term_id));
foreach ($tax_terms as $tax_term) {
$count += wp_get_term_postcount_recursive($tax_term->term_id);
}
return $count;
}
function wp_get_term_postcount_recursive($term_id, $excludes = array()) {
$count = 0;
foreach ($tax_terms as $tax_term) {
$tax_term_terms = get_terms($tax_term->name, array(
'child_of' => $tax_term->term_id,
'exclude' => $excludes,
));
$count += $tax_term->count;
$excludes[] = $tax_term->term_id;
$count += wp_get_term_postcount_recursive($tax_term->term_id, $excludes);
}
return $count;
}
The recursive function is there to prevent double counting of childs. You can add those two functions inside functions.php.
Then update your code to use it:
$categories = get_terms($taxonomy, $args);
foreach($categories as $categ) {
print $categ->name.' / 'wp_get_term_postcount($categ);
}
I'm new at Drupal 7, so I have a question.
I have my own content type Writers that includes such fields as Title, Years of life, Photo, Description.
I have a task to display 3 random Writers at page. Actual I've done it with help of Views module, but I want to do it myself.
So I created my own module random_content like that:
<?php
function random_content_help($path, $arg) {
switch ($path) {
case "admin/help#random_content":
return '<p>'. t("Displays random content") .'</p>';
break;
}
}
function random_content_block_info() {
$blocks['random_content'] = array(
'info' => t('Random content'),
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
function random_content_contents() {
$query = db_select('node', 'n')
->fields('n', array('nid', 'title'))
->condition('type', 'writers')
->orderBy('rand()')
->range(0,3)
->execute();
return $query;
}
function random_content_block_view($delta = '') {
switch($delta){
case 'random_content':
$block['subject'] = t('Random content');
if(user_access('access content')) {
$result = random_content_contents();
$items = array();
foreach ($result as $node){
$items[] = array(
'data' => l($node->title, 'node/' . $node->nid) . '</br>',
);
}
if (empty($items)) {
$block['content'] = t('No data availible.');
} else {
$block['content'] = theme('item_list', array(
'items' => $items));
}
}
}
return $block;
}
As you can see, I've learned only to add links to particular content. But how can I display full information including Title, Years of life, Photo and Description?
To display the full node, or parts of it you need to load the node. E.g.
$my_node = node_load($nid);
$render_array = array();
$render_array['title'] = array(
'#type' => 'markup',
'#markup' => $my_node->title
);
$author = field_get_items('node', $my_node, 'field_author','und');
$render_array['author'] = array(
'#type' => 'markup',
'#markup' => $author[0]['safe_value']
);
// or as some like to do it
$render_array['author'] = array(
'#type' => 'markup',
'#markup' => $my_node->field_author['und'][0]['value']
);
echo drupal_render($render_array);
Note the 'und' constant means the language is undefined. If you have translation/language enabled and different content for different languages you would have to use 'en', 'de' etc. for the appropriate language.
You can also let drupal render the node and then manipulate or retrieve individual items. Like this
$my_node = node_load($nid);
$build = node_view($my_node,'full');
$build['body'][0]['#markup'] = $build['body'][0]['#markup'].' some addition';
$build['field_author'][0]['#markup'] = $build['field_author'][0]['#markup'].' my favorite';
echo drupal_render($build);
The advantage of using this latter method is that then the whole themeing engine kicks in, and all hooks that are set to act on the content etc. Of course, if you only want to retrieve values you don't need that.
Note also, I assume your author field is named field_author. You should check that in the field edit window for the content type.
I have taxonomy with 3 levels of children. I need to get all level of children by a parent tid.
taxonomy_get_children($tid) - gives a direct children of that particular $tid only. But not the all grand children.
How do I process this?
Thanks,
function taxonomy_get_children_all($tid, $vid = 0, $key = 'tid'){
$c = taxonomy_get_children($tid, $vid, $key);
$result = array();
foreach ($c as $t => $d){
$result[$t] = $d;
$below = taxonomy_get_children_all($t, $vid, $key);
if (!empty($below)) {
foreach ($below as $nt => $nd){
$result[$nt] = $nd;
}
}
}
return $result;
}
Reference: https://drupal.org/node/381952
Looking on https://api.drupal.org/api/drupal/modules%21taxonomy%21taxonomy.module/function/taxonomy_get_children/7
and
https://api.drupal.org/api/drupal/modules%21taxonomy%21taxonomy.module/function/taxonomy_get_children/6
You should get multiple results. What Drupal do you use ?
$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('MACHINE_VOCABULARY_NAME');
foreach ($tree as $term) {
$termID[] = array(
'termId' => $term->tid,
'parent'=> $term->parents,
'depth' => $term->depth
);
}
dump($termID);
I have this piece of code to count the number of posts in a custom taxonomy term and its child terms:
function wp_get_postcount($id)
{
$count = 0;
$taxonomy = 'productcategories';
$args = array(
'child_of' => $id
);
$tax_terms = get_terms($taxonomy,$args);
var_dump($tax_terms);
foreach ($tax_terms as $tax_term) {
$count +=$tax_term->count;
}
return $count;
}
The problem is, it returns as a null for the actual term that contains the post, because that has no child terms. I'd like to know if its possible for the query to include the term with the $id and its children as well?
Thank you!
Why not use 'pad_counts' in get_terms.
$terms=get_terms('my_taxonomy',array('pad_counts'=>1));