Find out a posts taxonomy - wordpress

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.

Related

Retrieve custom post type specific terms from a taxonomy that is being used by multiple custom post types

I have a custom taxonomy (tax_classes) that has been registered to two custom post types (cpt_events and cpt_galleries). On the 'index' page for each of the custom post types I want to get the terms from the tax_classes taxonomy for that specific CPT. For example on the events CPT I want to show the terms that have been used by its posts post only.
I have looked into get_terms('tax_classes'); but this gives me ALL the terms for the taxonomy. Unfortunately there isn't a 'post_type' argument for the get_terms WP function to help with the filtering.
Any help on this would be greatly appreciated
Thanks
J
<?php
$desc = wp_get_object_terms( $post->ID, 'your-taxonomy' );
if ( ! empty( $desc ) ) {
if ( ! is_wp_error( $desc ) ) {
foreach( $desc as $term ) {
echo $term->name;
}
}
}
?>
$post->ID knows the custom-post-type of the page you are on. The above code will look for your-taxonomy and echo it out.
Have you looked at get_object_taxonomies(); ?
https://codex.wordpress.org/Function_Reference/get_object_taxonomies
Its first parameter is the custom post type and the second the taxonomy.

Woocommerce Conditional Tags in Functions.php

I have the following code to echo an announcement when a customer visits a specific product category, but I can't get it to work:
function gvi_announcement() {
if ( is_product_category( 'accessories' ) ) {
echo '<p class="my-alert"><span>This is an accessory</span></p>';
};
}
add_action ('woocommerce_single_product_summary', 'gvi_announcement' , 10);
It works fine without the conditional tag, but I've exhausted every other way to make this work using conditions.
UPDATE:
Thanks for your suggestion #Ibrahim, but I still cannot get it to work. Here's my code now:
function gvi_announcement() {
global $post;
$terms = get_the_terms( $post->ID, 'accessories' );
if ( $terms ) {
echo '<p class="my-alert"><span>This is an accessory</span></p>';
};
}
add_action ('woocommerce_single_product_summary', 'gvi_announcement' , 10);
The condition is_product_category is used to check if the page you are viewing is a product category page or not and the action woocommerce_single_product_summary is used on single product page. So the condition will invariably fail.
In a single product page, you will have to use get_the_terms to get the product categories to which the product belongs to. Something like this :
global $post;
$categories = get_the_terms( $post->ID, 'product_cat' );
You can then apply your conditions.

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;
}
}
}
?>

Getting WordPress to automatically show category permalink for only specific categories

I have searched, here is the closes result.
I am building a new wordpress site. I want most posts to have no category in the URL, simply www.site.com/title. However I do want the blog posts to be separate, so I'd like www.site.com/blog/title. I'd also like the option to add more like that in the future, for only specific categories, not the entire site.
There are many questions similar to this here on stackoverflow but most have 0 replies. Any advice would be great. I've even tried Advanced Permalinks without any luck.
You can simply do that by Setting > Permalinks and add to Common Setting > Custom Structure the value /blog/%postname%/. There you will get the blog post accessible from www.site.com/blog/title.
I cannot understand the first question. By:
I want most posts to have no category in the URL
do you mean to NOT HAVING www.site.com/category/category-name? or not having www.site.com/category/post?
EDIT #1
To answer this:
www.site.com/category/post is what I ONLY want for blog posts with the category of "Blog" > if the category is "Shoes" I don't want the category displayed in the URL. –
First: you can set the Permalink to /%postname%/ so all your post will have site/title therefore accessible from that link
Second: You have to filter the permalink to behave differently for the posts under "Blog" category.
Try this
add_filter( 'post_link', 'custom_permalink', 10, 2 );
function custom_permalink( $permalink, $post ) {
// Get the categories for the post
$categories = wp_get_post_categories( $post->ID );
foreach ( $categories as $cat ) {
$post_cat = get_category( $cat );
$post_cats[] = $post_cat->slug;
}
// Check if the post have 'blog' category
// Assuming that your 'Blog' category slug is 'blog'
// Change 'blog' to match yours
if ( in_array( 'blog',$post_cats ) ) {
$permalink = trailingslashit( home_url( 'blog/' . $post->post_name ) );
}
return $permalink;
}
Third: You have to filter rewrite_rules
add_filter( 'rewrite_rules_array', 'custom_rewrite_rule' );
function custom_rewrite_rule( $rules ) {
$new_rules = array(
'blog/([^/]+)/trackback/?$' => 'index.php?name=$matches[1]&tb=1',
'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
'blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?name=$matches[1]&cpage=$matches[2]',
'blog/([^/]+)(/[0-9]+)?/?$' => 'index.php?name=$matches[1]&page=$matches[2]'
);
$rules = $new_rules + $rules;
return $rules;
}
Go to permalink setting and save the setting to refresh your rewrite rules and make the changes above active
NOTE: Add those functions on your active theme functions.php template
NOTICE: I haven't test it yet but that's the way you change permalink. I did the similar way to change my permalink on archives and search result.

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

Resources