Current taxonomy item in custom taxonomy term page - php

I've got a custom taxonomy called trailer_type with terms long and short. When visiting my_site/trailers/short (a page powered by template taxonomy-trailer_type.php) I display my taxonomy terms this way:
$terms = get_terms( 'trailer_type' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
Works well but is there a simple way to add a "current" class? For example if I'm on the "long" page, I'd need "long" to have "current" class in this menu.

You need to look at the queried object. There's several ways to do it, here's what I tend to use: $wp_query->queried_object - this returns, as the name implies, the queried object.
In your case, something like this should work:
$curTerm = $wp_query->queried_object;
$terms = get_terms( 'trailer_type' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
echo '<ul>';
foreach ( $terms as $term ) {
$classes = array();
if ($term->name == $curTerm->name)
$classes[] = 'current';
echo '<li class="'. implode(' ',$classes) .'">' . $term->name . '</li>';
}
echo '</ul>';
}
I'm setting the classes to an array simply for future expansion. You could set it to a string right away as well.

Try using
get_query_var( 'term' )
and check
if ($term->name == get_query_var( 'term' )){
//make something
}

Related

Custom dropdown list of terms as links (woocommerce, wordpress)

I want to add a custom dropdown list of an attribute (in this case it's the brand), with the options lead to the attribute page as a link.
I got this working
add_filter('woocommerce_before_shop_loop','wc_reg_for_menus', 1, 2);
function wc_reg_for_menus() {
$terms = get_terms( 'pa_marke' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<select>';
foreach ( $terms as $term ) {
echo '<option value="'.$term->name.'">'.$term->name.'</option>';
}
echo '</select>';
}
}
And I think I somehow need to add this part
get_term_link( WP_Term|int|string $term, string $taxonomy = '' )
Thank you!
Felix
Your code works well for getting values for a specific taxonomy (attribute).I only changed the get_terms function.
I also added some data attributes to get the taxonomy and slug of each term (option).
add_filter( 'woocommerce_before_shop_loop','wc_reg_for_menus' );
function wc_reg_for_menus() {
$terms = get_terms( array(
'taxonomy' => 'pa_marke',
'hide_empty' => false,
));
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<select id="shop_pa_marke">';
foreach ( $terms as $term ) {
echo '<option value="' . $term->name . '" data-taxonomy="' . $term->taxonomy . '" data-slug="' . $term->slug . '">' . $term->name . '</option>';
}
echo '</select>';
}
}
Then you will need to use a jQuery script to redirect the user based on the chosen attribute option.
add_action( 'wp_footer', 'redirect_after_select_option' );
function redirect_after_select_option() {
?>
<script type="text/javascript">
jQuery( function($){
$('#shop_pa_marke').change(function(){
const url = window.location.href;
const taxonomy = $(this).children("option:selected").data('taxonomy').replace('pa_','filter_');
const slug = $(this).children("option:selected").data('slug');
let urlParams = new URLSearchParams(window.location.search);
urlParams.set(taxonomy, slug);
window.location.replace( '?'+urlParams.toString() );
});
});
</script>
<?php
}
The code has been tested and works. Add it to your active theme's functions.php.
Thank you!
Just now I found a solution without jQuery, that works quite fine as well:
add_filter('woocommerce_before_shop_loop','wc_reg_for_menus', 1, 2);
function wc_reg_for_menus() {
$terms = get_terms( 'pa_marke' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<select onchange="location = this.value;">';
foreach ( $terms as $term ) {
echo '<option value="'.get_term_link( $term ).'">'.$term->name.'</option>';
}
echo '</select>';
}
}
Only after the new page loaded, the first option of the menu is selected again, but that´s ok for me, because it won't be displayed there later on.

Show Woocommerce subcategories on single page by their parents slug or ID

I am trying to add Publisher, Topic and Author to a Single Product with help of categories/subcategories. This is how it looks after hours of coding/and copying (very fresh with WooCommerce tbh)
This is what I am getting, but it shows ALL subcategories, not only the ones associated to the Product, this is the code I am using
function get_product_subcategories_list( $category_slug ){
$terms_html = array();
$taxonomy = 'product_cat';
// Get the product category (parent) WP_Term object
$parent = get_term_by( 'slug', $category_slug, $taxonomy );
// Get an array of the subcategories IDs (children IDs)
$children_ids = get_term_children( $parent->term_id, $taxonomy );
// Loop through each children IDs
foreach($children_ids as $children_id){
$term = get_term( $children_id, $taxonomy ); // WP_Term object
$term_link = get_term_link( $term, $taxonomy ); // The term link
if ( is_wp_error( $term_link ) ) $term_link = '';
// Set in an array the html formated subcategory name/link
$terms_html[] = '' . $term->name . '';
}
return '<span class="subcategories-' . $category_slug . '">' . implode( ', ', $terms_html ) . '</span>';
}
add_action('woocommerce_single_product_summary','monolith_cat_scan', 31);
function monolith_cat_scan() {
echo '<p>Topic : ';
echo get_product_subcategories_list( 'topics' );
echo '</p>';
echo '<p>Publisher : ';
echo get_product_subcategories_list( 'publishers' );
echo '</p>';
echo '<p>Author: ';
echo get_product_subcategories_list( 'authors' );
echo '</p>';
}
But I can't get the whole thing to work like I want to and get the subcategories of the Single Product, in this example only Spirituality, SOUNDS TRUE INC (only sub cat in Publishers), and Allan Watts should be there.
I'd appreciate every help!
I got a working code (it doesn't look beautiful I know but it's the best I could do and it does the trick.
add_action('woocommerce_single_product_summary','monolith_cat_scan', 31);
function monolith_cat_scan() {
global $post;
$cats = get_the_terms( $post->ID, 'product_cat' );
if ( ! empty( $cats ) ) {
foreach ( $cats as $term ) {
if( $term->parent == 30 ) {
echo '<p>Topic : ' . $term->name . '';
}
}
}
}
add_action('woocommerce_single_product_summary','monolith_cat_scan2', 32);
function monolith_cat_scan2() {
global $post;
$cats = get_the_terms( $post->ID, 'product_cat' );
if ( ! empty( $cats ) ) {
foreach ( $cats as $term ) {
if( $term->parent == 31 ) {
echo '<p>Author : ' . $term->name . '';
}
}
}
}
add_action('woocommerce_single_product_summary','monolith_cat_scan3', 33);
function monolith_cat_scan3() {
global $post;
$cats = get_the_terms( $post->ID, 'product_cat' );
if ( ! empty( $cats ) ) {
foreach ( $cats as $term ) {
// If parent cat ID = 116 echo subcat name...
if( $term->parent == 32 ) {
echo '<p>Publisher : ' . $term->name . '';
}
}
}
}

How to add "else" to "foreach"?

i don't know how to add else to foreach!
here is my code:
<?php $terms = get_the_terms( $post->ID , 'actor' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'actor' );
if( is_wp_error( $term_link ) )
continue;
echo '' . $term->name . '';
}
?>
You could try something similar:
<?php
$terms = get_the_terms( $post->ID , 'actor' );
if ($terms) {
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'actor' );
if ( is_wp_error( $term_link ) )
continue;
echo '' . $term->name . '';
}
} else {
// do the work for else
}
?>
For sake of completeness, there is an old RFC (10 years old) that proposes loop+else:
https://wiki.php.net/rfc/loop_else
foreach is a loop. else is part of a conditional statement. It looks like you just want a conditional inside your loop. This would just be a standard conditional.
if ($a > $b) {
echo "a is greater than b";
} else {
echo "a is NOT greater than b";
}
I assume you want to break the loop in that case.

Retrieve single tag among all tags

I need help with my wordpress site. While displaying "tags" on a product page,
<?php echo $product->get_tags(', ', '<span class="tagged_as">' . _n( 'Tag:', 'Tags:', $tag_count, 'woocommerce' ) . ' ', '</span>' ); ?>
is used to show them.
However, I need product page to show only 1 tag for a product. This code makes all tags to appear on the page. Which code should I use or how should I edit this?
in example:
if a product has the tags: summer, winter, fall, spring
I want only "fall" to be seen
<?php
$tags = get_terms( 'product_tag' );
$first = true;
foreach ($tags as $tag) {
if ( $first ){
$term_link = get_term_link( $tag );
if ( is_wp_error( $term_link ) ) {continue;}
echo '' . $tag->name . '';
$first = false;
}
}
?>
This will only print the first tag in a woocommerce product page. Its tested and working.
If you want to display any selected tag, then you need to define which tag you want to be visible. Maybe using a custom field. Lets say you have a custom field name 'selected_tag' using ACF plugin and it will print the exact tag name that you want to visible in the product page.
<?php
$selected_tag = get_field('selected_tag');
$tags = get_terms( 'product_tag' );
foreach ($tags as $tag) {
if ( $tag->name == $selected_tag ){
$term_link = get_term_link( $tag );
if ( is_wp_error( $term_link ) ) {continue;}
echo '' . $tag->name . '';
}
}
?>

Display a list of taxonomies, only if they are hierarchical

I am trying to show a list of the taxonomies a custom post type is in, excluding those which aren't hierarchical.
The code below currently works but shows all taxonomies, however I can't get it to check if the taxonomy is hierarchical before running it through the loop.
I know there is a WordPress function that runs this check, but as I usually work front-end, I can't seem figure out where to put it in order for it to take effect:
is_taxonomy_hierarchical( $taxonomy )
The following is the function I am using to output a list of the taxonomies:
// get taxonomies terms links
function custom_taxonomies_terms_links(){
// get post by post id
$post = get_post( $post->ID );
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$out = array();
echo '<a href="';
echo '/';
echo '">';
echo 'Home';
echo "</a> / ";
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if (!empty( $terms )) {
foreach ( $terms as $term ) {
$out[] =
'<a href="'
. get_term_link( $term->slug, $taxonomy_slug ) .'">'
. $term->name
. "</a> / ";
}
$out[] = " ";
}
}
return implode('', $out );
}
If I understand your correctly, couldn't you just test the taxonomy like the following:
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
if ($taxonomy->hierarchical) {
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if (!empty( $terms )) {
foreach ( $terms as $term ) {
$out[] =
'<a href="'
. get_term_link( $term->slug, $taxonomy_slug ) .'">'
. $term->name
. "</a> / ";
}
$out[] = " ";
}
}
}
When you use 'objects' as the second parameter in:
get_object_taxonomies( $post_type, 'objects' );
what you get back is an array of taxonomy objects, as opposed to just taxonomy names (the other option). The taxonomy object has a property "hierarchical" that indicates whether that taxonomy is hierarchical or not. You can test this to choose the types of taxonomies (hierarchical or not) that you want.

Categories