link to custom taxonomy by id - wordpress

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';

Related

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>

Get attachments based on a custom post types post ID

I don't know if this is possible but I'll try to explain.
I have a custom post type (creatives) and a taxonomy (image-sort). Every post in the CPT is a person (a creative). They can upload images and sort them by choosing which categories (the 'image-sort' taxonomy) they belong to.
There are multiple creatives, and they will post images to different categories. Some to all of them, some to just a few.
Every creative have their own page with a dynamic listing of which categories they have posted content to. My problem is though that if one creative have posted to 'cat-1' and 'cat-2' everybody get that listed out on their respective page. What I want is to only show 'cat-1' and 'cat-2' on the creative which has posted to those categories. If another creative has posted to 'cat-1' and 'cat-3' I only want those two to appear on his page.
Does this make sense?
<ul>
<?php
$terms = get_terms('image-sort');
if ( $terms && !is_wp_error($terms) ) :
foreach ( $terms as $term ) :
?>
<li><?php esc_html_e($term->name); ?</li>
<?php endforeach; endif; ?>
</ul>
Not sure if i understand you, but if i do, try replacing this line:
$terms = get_terms('image-sort');
with the following line:
$terms = wp_get_object_terms(get_the_ID(), 'image-sort');
EDIT
Try fetching the terms with the following piece of code:
<?php
// get all attachments of the current post
$attachments = get_children('post_parent=' . get_the_ID() . '&post_type=attachment&post_status=any&posts_per_page=-1');
// we're saving the terms here
$all_terms = array();
// looping through all attachments
foreach( $attachments as $attachment) {
$terms = wp_get_object_terms($attachment->ID, 'image-sort');
if ($terms) {
// looping through all attachment terms and adding them to the main array
foreach ( $terms as $term ) {
$all_terms[$term->term_id] = $term;
}
}
}
?>

WordPress get category ID from URL

I have hard time trying to find the solution, does somebody know how to get this:
I have one WordPress post in more than one category, but only one is permalink category. I need to get the ID only of that permalink category (I need this info so I can take few latest posts from permalink category via custom query).
url looks like this http://domain.com/category-name/post-title
I need that "category-name" ID.
A Good one to use is:
<?php $page_object = get_queried_object(); ?>
<h1><?php echo $page_object->cat_name; ?></h1>
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) ); get url
$url_array = explode('/',$current_url);
$retVal = !empty($url_array[5]) ? $url_array[5] : $url_array[4] ;
$idObj = get_category_by_slug($retVal);
echo $idObj->name
My answer is:
function get_category_by_url($url) {
foreach( (get_the_category()) as $category) {
if ( get_category_link($category->cat_ID) == $url )
return $category->slug;
}
return false;
}
$category = end(get_the_category());
$current = $category->cat_ID;
echo 'id='.$current . ' - name=' . $category->cat_name;
If the post belongs to many categories, what if you're viewing the post from a second category. In that case retrieving the category ID of the permalink category may not help, since you would need the related posts of the current category in action.
For that, you can get the ID by passing the category name as follows:
<?php get_cat_ID($cat_name)?>
Does this help?
Wordpress chooses the oldest category as the permalink category. There's no way to change that behavior unless you use some plugin. If you choose to use a plugin you make take the category ID from plugin settings.
You can list all categories of this post and choose most relevant category.
Use the following code inside The Loop:
foreach((get_the_category()) as $category)
{
if ( $category->cat_ID == 1000 )
; // DO SOMETHING
}

Find out a posts taxonomy

I have a posts page (using single.php )and I want to be able to some how query the post for which taxonomy category it's in. Is this possible? I've looked at every wp function under the sun and none seem to enable you to query for this.
I have used this on other types of pages:
if(is_tax('taxnamehere')) {
But when I get in to single.php (the post page) this no longer works
Found it.
$terms = get_the_terms( $post->ID , 'leadership' );
if(is_array($terms)) {
foreach( $terms as $term ) {
$tax = $term->slug;
unset($term);
}
}
returns the category within the taxonomy.

Resources