drupal 7 taxonomy_vocabulary_machine_name_load did not show the fields - drupal

I have created a vocabulary and listed terms within it, in D7. I have added a field named "Icon Color". Now, within the view, I have added the code block as below which perfectly displayed the fields like tid, name, description but it did not show the field "Icon Color".
<?php
$name = 'Programme';
$myvoc = taxonomy_vocabulary_machine_name_load($name);
$tree = taxonomy_get_tree($myvoc->vid);
foreach ($tree as $term) {
print_r($term);
}
?>
The fields are listed within the vocabulary as below:

You must load the taxonomy using term id by taxonomy term load function. Hope the below code helps you.
$name = 'YOUR_MACHINE_NAME';
$myvoc = taxonomy_vocabulary_machine_name_load($name);
$tree = taxonomy_get_tree($myvoc->vid);
foreach ($tree as $term) {
$term = taxonomy_term_load($term->tid);
print_r($term);
}

Related

Wordpress categories hierachy issue

I have problem with my catogories in my custom post type. I am trying to do a basic apartments booking system. I created custom post called Apartments and then I added a categories. The category tree looks like this:
Countries
- Spain
-- Barcelona
-- Madrid
- Greece
-- Athenes
and so on.
I want to achive effect when the user enter to the apartment in Barcelona, the higher category will be displayed in header (in this example Spain). When the user eneter to the apartment in Athenes the Greece category should be in header. I tried solution that I have found on stack but it does not work for me.
$category = get_the_category();
foreach ($category as $cat) {
echo $cat->name;
}
$ancestors = get_ancestors( $category[0]->term_id, 'category' );
$direct_parent_id = $ancestors[0];
echo $direct_parent_id;
Only the second category is displayed and as you can see the categories are displayed in alphabetical order instead of hierarchical.
I hope you understand what I mean.
Thank you in advance for directions
Function get_the_category returns the object WP_Term. So, it's possible to get the parent from that object.
Used this answer, in your case it will be:
First, add this function to your functions.php file of active theme/child theme to have access to it from other files of theme/child theme:
function get_deep_child_category( $categories )
{
$maxId = 0;
$maxKey = 0;
foreach ( $categories as $key => $value )
{
if ( $value->parent > $maxId )
{
$maxId = $value->term_id;
$maxKey = $key;
}
}
return $categories[$maxKey];
}
Note: in some cases this will not work as expected. For example, if your post has category Some_category, which is child of Some_category_parent, and the Some_category_parent was created after your Spain category, then, this will return the category Some_category.
Next step will be to use it. For example, in your post template:
$categories = get_the_category();
if ( $categories ){
$deep_child = get_deep_child_category( $categories );
echo get_cat_name($deep_child->parent);
}
After getting the deepest category object with function get_deep_child_category, we got the ID of it's parent category. get_cat_name() returned the name of parent category using it's ID. In your case, it's Spain.

get a field of wordpress custom taxonomy term

I have registered a new taxonomy and assigned a custom field (image) to its term (subcategory) using Advanced Custom Fields 4.4.0. Somehow I can't get it displayed on custom page template.
$term = get_query_var('term');
$term = get_term_by('slug', $term, 'gallery');
$termid = $term->term_id;
get_field('featured_image', $termid);
What am I doing wrong?
Try echoing 'get_field()' or store it in a variable then echo it.
$custom = get_field('featured_image', $termid);
echo $custom;
Your code is right. But you must echo or print to write to screen. Or you can using the_field(); function.

Can't retrieve custom taxonomy within multisite

I'm currently working on a Wordpress multisite. While I can retrieve custom post types from within child blogs by using switch_to_blog(1) I cannot retrieve any custom taxonomies this way.
For instance, this page on the main blog lists posts from the "employment" post type and shows the custom "location" category associated with it.
http://209.59.177.85/employment-opportunities/
This one is within a child blog, and the template code is after a call to switch_to_blog(1):
http://209.59.177.85/waterloo/careers/
Same code, but the custom post type shows, and the custom taxonomy does not. The page does not generate any errors. I can't even get a basic term list printed on the page for the taxonomy.
Has anyone done this before?
Thanks!
Here's the solution, in case anyone is looking. The terms can, of course, be accessed directly from the db:
<span class="date">Location: <?php
$jobid = get_the_ID();
$queryterms = "
SELECT *
FROM ".$table_prefix."terms terms, ".$table_prefix."term_taxonomy term_taxonomy, ".$table_prefix."term_relationships term_relationships
WHERE (terms.term_id = term_taxonomy.term_id AND term_taxonomy.term_taxonomy_id = term_relationships.term_taxonomy_id)
AND term_relationships.object_id = ".get_the_ID()."
";
$terms = $wpdb->get_results($queryterms, OBJECT);
if ( $terms != null ) {
foreach( $terms as $term ) {
echo "<a href='/location/";
print $term -> slug ;
echo "/'>";
print $term -> name ;
echo "</a>";
unset($term);
} } ?> | Posted on: <?php the_time('F j, Y'); ?></span>

link to custom taxonomy by id

Through a series of specific requirements, I find myself needing to link to a custom taxonomy category using its term id...
I've got this - which displays a link to all taxonomies - I wish to change it so it only displays a link to the taxonomy with the term id dynamically pulled from a custom field I'm using.
$taxonomy = 'event-categories';
$terms = get_terms($taxonomy);
if ($terms) {
foreach($terms as $term) {
echo '<li>' . $term->name .'</li>';
}
};
essentiall I need "link_to_taxonomy_category(x)" where x = term_id
Thanks
The function you are looking for is get_term_link. It takes either a term object, ID or slug and a taxonomy name and returns a URL to the term landing page.
As a side note hard coding the link as you have in the example above is fragile -- always keep your code as portable as possible. If the site is moved to a different domain, that link will break. WordPress has several functions that generate links dynamically based on the current installation environment. get_term_link is one example.
From the Codex:
$terms = get_terms('species');
echo '<ul>';
foreach ($terms as $term) {
echo '<li>'.$term->name.'</li>';
}
echo '</ul>';
If you have single term_id e.g: 10, custom taxonomy series then you can use the following code to get the taxonomy term link.
note : change 10 to your variable for term_id and 'series' to your taxonomy.
$term = get_term( 10, 'series' );
$term_link = get_term_link( $term );
echo 'View All';

How to retrieve the ID of the page the user is currently viewing in Wordpress sites?

I am developing a Wordpress plugin in which I am displaying the Archive based on Categories.
Categories are my pages in my site and each Page should display post in the Archive belongs to that Category only. So i need to dynamically get the Category ID of the page the user is currently viewing.How can I retrieve it?
You're using the word page a little confusingly there - are you talking about WordPress pages that you have created in the admin, or the webpage displayed for each category archive?
If the latter, you can use the global $wp_query to get the category ID like so;
$cat_ID = $wp_query->get_queried_object_id();
Remember that a post can belong to more than one category.
This code might work for you:
if(is_category()){
$cat_id = get_query_var('cat');
} else if (is_single()) {
$cat_id = '';
foreach (get_the_category() as $catt) {
$cat_id .= $catt->cat_ID.' ';
}
$cat_id = str_replace(" ", ",", trim($cat_id));
}
if (!intval($cat_id)) $cat_id='';
$query = "&category=$cat_id";
$posts = get_posts($query);
$postlist = '';
foreach ($posts as $post) {
// something for each post
}

Resources