Get Custom Taxonomy ID Assigned To Page - php

I am trying to get the term ID of the custom taxonomy a page on my website is assigned to.
So I have a page that's assigned to a term "Seventeen" under the custom taxonomy "Clip Category". So I'm looking for the ID of the term seventeen.
Here's the code I've got so far with some help from one kind StackExchange use:
<?php
$slug = $post->post_name;
echo $slug;
$term = get_query_var($slug);
echo $term;
$taxonomy = get_query_var('clipcat');
print_r($taxonomy);
$termid = get_term_by( 'slug', $term, $taxonomy );
echo $term->term_id;
?>
The code gets stuck right after echoing the $slug variable, as can be seen on this page: http://noellesnotes.com/portfolio/seventeen/

You do it wrong. $slug = $post->post_name;is mean you take slug of page, not slug of term.
You should take all terms from page <?php get_the_terms( $post_id, $taxonomy ); ?> and then you can work with it. You will stuck in trouble fell free to ask me. Good luck.

You can use get_query_var to retrieve the public query variable in the WP_Query class of the global $wp_query object
You'll need the term and taxonomy name. The best practice to keep this dynamic is to use get_query_var('term') to get the term slug and get_query_var('taxonomy') to get the taxonomy slug. This will be fed back into get_term_by to get the term ID of the current term being displayed
<?php
$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
echo $term->term_id;
?>

Related

Get and display the product category featured image in Woocommerce

I'm making my first theme and everything is progressing real fast thanks to all the assistance offered by The Loop and WooCommerce's SDK. Then today I spent an entire day failing to do something as simple as showing an image... After an entire day of struggling the only things I managed to learn is that WP seems to offer NO means for fetching a category's image and MANY people have asked this question for many years and STILL I can't find a way to ACTUALLY do it...
:(
What I want to do is create a slider above my store that shows the images of a curated selection of shop categories. I want to be able to enter a list of term names and based on that my function should generate links to the product categories in the form of the category's image.
Sounds simple... turns out, it's nowhere near CLOSE to simple... Before you go off and mark this as a duplicate question, let me explain why I am asking it...
Some of the solutions I have found require that I know the term's IDs, not the tern name. Some say "Get the id's using a custom term search" but doesn't explain how. I know how to do custom taxonumy based queries for posts but not for terms. the documentation confuses me in terms of what values to pass to query terms :(
Other solutions require that I first find a product of a specific category and then find the product's category image working backwards from there (????)
Then of course there is the default answer people love to give for everything: "Oh you are designing your own theme and want to show category icons? Simple, just download a plugin to do that for you". Now why didn't I think of just including someone else's plugin into my theme? (facepalm)
Other answers just show how to print the list of term names but nothing so far has been able to let me do what should have been been as simple as this:
$categories = array("software", "plugins", "merch");
foreach($categories as $cat) {
$term_id = get_term_id($cat);
$term_image = get_term_featured_image($term_id);
echo '<img src="'.$term_image.'">;
}
First problem with getting the term is that the wordpress function to get the term id only works on the category taxonomy but I need to query WooCommerce's product_cat taxonomy. Secondly there doesn't seem to be an option to fetch the thumbnail/featured image even if you HAVE an id. So now what?
So I went low level and started querying the tables directly using $wpdb and I determine the term I am after has term_id 94. I query the termmeta table for the thumbnail's post ID and I find it is 905. Now I turn to my posts table and find.... there IS no post entry 905! WTF? So I do this for two more categories and find the same thing. Finding the image's id results in nothing being returned from the attempt at extracting the post's attachments since there IS no post matching the image's id...
Why is this so darned hard? WordPress makes everything else so incredibly simple yet this simple sounding task seems to be near impossible to do... so now that you see I have Googled this to death and struggled my backside off already, I am asking this question out of desperation:
How do I take an array of product_cat term names and convert that into an array of url's to display the category's image?
Thanks
The shortest way is to use woocommerce_subcategory_thumbnail() dedicated function:
$product_categories = array("software", "plugins", "merch");
// Loop through the product categories
foreach( $product_categories as $category ) {
// Get the WP_term object
$term = get_term_by( 'slug', sanitize_title( $category ), 'product_cat' );
// Get the term link (if needed)
$term_link = get_term_link( $term, 'product_cat' );
// Display the product category thumbnail
woocommerce_subcategory_thumbnail( $term );
}
The other step by step way, that will display the linked product category image with its name:
$product_categories = array("software", "plugins", "merch");
// Loop through the product categories
foreach( $product_categories as $category ) {
// Get the WP_term object
$term = get_term_by( 'slug', sanitize_title( $category ), 'product_cat' );
// Get the term link (if needed)
$term_link = get_term_link( $term, 'product_cat' );
// Get the thumbnail Id
$thumbnail_id = (int) get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
if( $thumbnail_id > 0 ) {
// Get the attchement image Url
$term_img = wp_get_attachment_url( $thumbnail_id );
// Formatted thumbnail html
$img_html = '<img src="' . $term_img . '">';
} else {
$img_html = '';
}
echo '' . $img_html . $term->name . '<br>';
}
Both works…
To get all product categories WP_Term objects and display them with their thumbnails:
// Get all product categories
$product_category_terms = get_terms( array(
'taxonomy' => "product_cat",
'hide_empty' => 1,
));
foreach($product_category_terms as $term){
// Get the term link (if needed)
$term_link = get_term_link( $term, 'product_cat' );
## -- Output Example -- ##
// The link (start)
echo '<a href="' . $term_link . '" style="display:inline-block; text-align:center; margin-bottom: 14px;">';
// Display the product category thumbnail
woocommerce_subcategory_thumbnail( $term );
// Display the term name
echo $term->name;
// Link close
echo '</a>';
}

Display Clickable list of a specific taxonomy in my custom post type

I'm having a hard time even coming up with a title for this.
If I'm editing the code for a regulator old blog post (content.php) and add
<?php the_category(', ') ?>
I get a clickable list of all the categories that post belong to.
I have a custom post type called research and a custom taxonomy associated with it called topics. I'm editing content-research.php I just want the topics to show up the same way when looking at a research post.
I tried
<?php the_topic(', ') ?>
and that was a complete failure.
So I'm hoping there is a simple solutions because I have some additional taxonomies for this post type I'd like to add as well.
the_category() function only searches terms from the 'category' taxonomy as you can see in the core file https://core.trac.wordpress.org/browser/tags/4.9.2/src/wp-includes/category-template.php line 75
You could write your own function or you can use this snippet:
$terms = get_the_terms( $post->ID , 'taxonomyname' );
foreach ( $terms as $term ) {
echo ''.$term->name.' ';
}
The answer from ovidiua2003 worked just needed this tweak.
echo ''.$term->name.' ';
So the whole thing would be
<?php
$terms = get_the_terms( $post->ID , 'taxonomy' );
foreach ( $terms as $term ) {
echo ''.$term->name.', ';
}
?>

How to access the names of the category name in Wordpress?

I'm trying to get the category name via the_slug() but I can't get it displayed via several methods. Does anyone know what's the right term?
You should use get_term_by. Example:
$term = get_term_by( 'slug', $value, $taxonomy );
echo $term->name;
So you have the slug but want to get the name?
$_cat = get_category_by_slug('category-slug'); // Returns Category Object
$_category_name = $_cat->name;
Source

Get the id of a WooCommerce product category by its name or slug

I have a wp template that I would like to assign to some pages. The template would ie. display all WooCommerce products that have the same master category name as the pages name itself.
By far I have tried using this code, but with no good output:
$idObj = get_category_by_slug($pagename);
$id = $idObj->term_id;
echo ": ". $id;
Unfortunately, the echo does not display anything.
Echoing $pagename works, and returns me the slug of the page.
Any good way I could make this work?
With a custom taxonomy is recommended to use get_term_by() instead :
$category = get_term_by( 'slug', $pagename, 'product_cat' );
$cat_id = $category->term_id
Reference: Get category ID from term slug…
To get a product category ID from term name, use:
$category = get_term_by( 'name', $pagename, 'product_cat' );
$cat_id = $category->term_id
The code seems to be correct, try a var_dump to see what you are getting from get_category_by_slug($pagename)
$idObj = get_category_by_slug($pagename);
var_dump($idObj);

Display Custom Taxonomy Field in sidebar

Hi Guys, I'm trying to display a taxonomy field called clpr_store_video in my Wordpress sidebar for each store is it possible to call the field value outside the loop on the sidebar, but yet attached to the post id?
I don't really know if its possible or how to do that.
EDITED
What I've tried from another tutorial but no luck.
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$subheading = get_field('clpr_store_video', $taxonomy . '_' . $term_id);
echo $subheading;
See code block to display taxonomy associated with post and list of taxonomies and their links:
If you wish to get taxonomy associated with particular post
$terms = get_the_terms( get_the_ID(), 'clpr_store_video' );
foreach ( $terms as $term ) {
echo $subheading = $term->name;
}
If you wish to get list and link of all taxonomies
<ul>
<?php
$taxonomies = get_terms('clpr_store_video', array("fields" => "all"));
foreach ($taxonomies as $taxonomy) {
echo '<li>'.$taxonomy->name.'</li>';
}
?>
</ul>

Categories