How Can I get term/ category name wordpress - php

I am using WordPress plugin Taxonomy images. I'm using it to get images for taxonomies.
From the below code I am getting the images and link. I want to get category name with these images. Here is my code. How can I get category name?
<?php
$terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'category', ) );
if ( ! empty( $terms ) ) {
print '<div class="row">';
foreach ( (array) $terms as $term ) {
print '<div class="col-sm-3"><a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, ' ' ) . '</div>';
}
print '</div>';
}
?>

Related

Exclude a specific taxonomy name

So I'm using wordpress and ACF. I created a CPT called products and a custom taxonomy named product_types. I have a loop that displays all product_type sections and each section contains products tied to it. So I'm looking for ways to easily filter new products or popular products thats why I come up with adding a dedicated category for it but I dont want it to exclude it from my post loop. pls see img attached
enter image description here
Here's the code I'm working with atm
<?php
$cpt_name = 'products';
$taxonomy_name = 'product_type';
// Retrieve the terms in the above taxonomy.
$terms = get_terms( $taxonomy_name );
if ( empty( $terms ) || is_wp_error( $terms ) ) {
return;
}
echo '<div class="products-group">';
foreach( $terms as $term ) {
echo '<div class="products-category">';
echo '<h2 class="title-category">' . $term->name . '</h2>';
$args = array(
'post_type' => $cpt_name,
'tax_query' => array(
array(
'taxonomy' => $taxonomy_name,
'field' => 'slug',
'terms' => array( $term->slug ),
),
),
'posts_per_page' => -1,
'category__not_in' => array( 30 ),
);
$query = new WP_Query( $args );
echo '<div class="products-list">';
while ( $query->have_posts() ) {
$query->the_post();
$attachment_id = get_field('header_bg_image');
$size = "wide-thumbnail"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
if ( ! empty( $image ) ) {
$image_html = esc_url( $image[0] );
} else {
$image_html = 'https://rasons.ltd/wp-content/uploads/2020/02/interior-exterior-finish-800x400.jpg';
}
printf( '%s', get_the_permalink(), esc_attr( the_title_attribute( 'echo=0' ) ), get_the_title() );
}
echo '</ul>';
wp_reset_query();
echo '</div></div>';
} // end foreach.
echo '</div>';
?>

get_terms with Redis Object Cache question

By default, hide_empty => is set to be true. If Redis object cache is disabled, the below code works just fine. But when Redis is enabled, the empty term will not be hidden. It just shows all the terms. I have tried to flush the cache. The issue remains.
Any idea what may cause the issue? thanks
$parentid = 182;
$args = array(
'parent' => $parentid
);
$terms = get_terms( 'product_cat', $args );
if ( $terms ) {
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>';
echo '<a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
echo $term->name;
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
I managed to solve it with term count
$parentid = 182;
$args = array(
'parent' => $parentid
);
$terms = get_terms( 'product_cat', $args );
$count = count($terms);
if ( $terms ) {
echo '<ul>';
foreach ( $terms as $term ) {
if ($term->count > 0){
echo '<li>';
echo '<a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
echo $term->name;
echo '</a>';
echo '</li>';
}
}
echo '</ul>';
}

Exclude certain taxonomy from function

I need a help with a WordPress code.
On this page https://developer.wordpress.org/reference/functions/get_the_terms/ I got a code that returns my taxonomies and their terms.
But I'd like a certain taxonomy called "Ad Type" to be excluded from this list.
I managed to exclude some terms from "Ad Type" using this code...
// An array of IDs to ignore / exclude
$ excluded_ids = array (1, 2, 3, 4);
foreach ($ terms as $ term) {
// Only proceed if the term_id is NOT in the $ excluded_ids array
if (! in_array ($ term-> term_id, $ excluded_ids)) {
$ out. = '<li> <a href="' .get_term_link($term-> slug, $ taxonomy).' '>'. $ term-> name. '</a> </ li>';
}
}
But what I want is that the "Ad Type" taxonomy does not appear at all.
Because with the code above the Title of "Ad Type" is still appearing on the terms list retrieved.
What i want is to totally exlcude this taxonomy from being retrieved on that part of my wordpress template.
Here is the code:
function wpdocs_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();
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
$out[] = "<h6>" . $taxonomy->label . ":</h6>\n<ul>";
foreach ( $terms as $term ) {
$out[] = sprintf( '<li>%2$s</li> - ',
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
$out[] = "</ul>\n";
}
}
return implode( '', $out );
}
And here is how i retrieve it on my template page:
<?php echo wpdocs_custom_taxonomies_terms_links(); ?>
Is there a way to do this?
can anybody help me?
Thanks
I don't know if I am comparing it right but you can check if current taxonomy is not Ad Type only then get terms
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
if ($taxonomy_slug !== 'ad_type') :
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
$out[] = "<h6>" . $taxonomy->label . ":</h6>\n<ul>";
foreach ( $terms as $term ) {
$out[] = sprintf( '<li>%2$s</li> - ',
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
$out[] = "</ul>\n";
}
endif;
}
You can exclude the desired term(s) by adding if ($term->term_id == 30) continue; as shown below, where 30 and 31 are the IDs of excluded terms.
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
$out[] = "<h6>" . $taxonomy->label . ":</h6>\n<ul>";
foreach ( $terms as $term ) {
//Excluded terms, change the IDs as you wish
if ($term->term_id == 30) continue;
if ($term->term_id == 31) continue;
$out[] = sprintf( '<li>%2$s</li> - ',
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
$out[] = "</ul>\n";
}
}
return implode( '', $out );
}

Get images from custom taxonomy terms

Ok, I don't know if asking a new question is allowed for this. But I think its a bit different than my previous question. (if not, please tell me).
Im trying to get featured images from my custom taxonomy terms. Yesterday I got pretty far, but my client wants to add the names of the team members. So I'm kind of back to the drawing board on this one.
My theme is made up out of four different collapsible panels. With a Custom Post type and a loop.
So I made a custom taxonomy ons_team because I do not want to show the team members on every panel. So I checked the boxes on the ctp where I want the team members to be shown.
But now that the client wants to add the names of the team members I'm forced to use a different code. Yesterday I got it to work with this code.
<?php $terms = get_the_terms( $post->ID , 'ons_team' );
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="ons-team">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'taxonomy' => 'ons_team',
) );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'ons_team' );
if( is_wp_error( $term_link ) )
continue;
}
?>
This only shows the team members on the panel in which I checked the boxes. But I cannot add the names of the team members to this code.
So I got it to work on the single page using this code:
<?php
// List of image links
$terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'ons_team' ) );
foreach( (array) $terms as $term) {
echo '<div class="col s6 m6 l3 teamimage"><a href="' . esc_attr( get_term_link( $term ) ) . '" title="' . sprintf( __( "%s" ), $term->name ) . '" ' . '>' . wp_get_attachment_image( $term->image_id, 'destacado-proyectos-home' ) . '</a>';
echo '<h2 class="truncate">' . sprintf( __( "%s" ), $term->name ) . '</h2>';
echo '</div>';
}
?>
But if I use that code on the panel I WANT the information to show up. It shows it across all the panels, and not on the only one I want to show it on.
I tried using a combination of both but then every panel still shows the images and names.
<?php $terms = get_the_terms( $post->ID , 'ons_team' );
$terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'ons_team' ) );
foreach( (array) $terms as $term) {
echo '<div class="col s6 m6 l3 teamimage"><a href="' . esc_attr( get_term_link( $term ) ) . '" title="' . sprintf( __( "%s" ), $term->name ) . '" ' . '>' . wp_get_attachment_image( $term->image_id, 'destacado-proyectos-home' ) . '</a>';
echo '<h2 class="truncate">' . sprintf( __( "%s" ), $term->name ) . '</h2>';
echo '</div>';
}
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'ons_team' );
if( is_wp_error( $term_link ) )
continue;
}
?>
Placing the code between the foreach or any other place kind of breaks the code. Is it possible that this doesn't work because I'm using $term / $terms alot?
Using it like this:
<?php $terms = get_the_terms( $post->ID , 'ons_team' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'ons_team' );
$terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'ons_team' ) );
foreach( (array) $terms as $term) {
echo '<div class="col s6 m6 l3 teamimage"><a href="' . esc_attr( get_term_link( $term ) ) . '" title="' . sprintf( __( "%s" ), $term->name ) . '" ' . '>' . wp_get_attachment_image( $term->image_id, 'destacado-proyectos-home' ) . '</a>';
echo '<h2 class="truncate">' . sprintf( __( "%s" ), $term->name ) . '</h2>';
echo '</div>';
}
if( is_wp_error( $term_link ) )
continue;
}
?>
Kind of works, but then it show the team member like 10 times in a row...
Any help is much appreciated!
Solved it!
Here is the correct code if people need it:
<div class="teamleden over-ons-ul">
<div class="center-align">
<div class="row">
<?php
$terms = get_the_terms( $post->ID , 'ons_team' );
if ( $terms != null ){
$terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'ons_team' ) );
foreach( (array) $terms as $term) {
echo '<div class="col s6 m6 l3 teamimage"><a href="' . esc_attr( get_term_link( $term ) ) . '" title="' . sprintf( __( "%s" ), $term->name ) . '" ' . '>' . wp_get_attachment_image( $term->image_id, 'destacado-proyectos-home' ) . '</a>';
echo '<h2 class="truncate">' . sprintf( __( "%s" ), $term->name ) . '</h2>';
echo '</div>';
}
foreach( $terms as $term ) {
unset($term);
} } ?>
</div>
</div>
</div>

WordPress, A page of thumbnails and titles for a grid of taxonomies for a custom post type

I am able to list the taxonomy children:
<?php
$taxonomy = 'store_name';
$tax_terms = get_terms($taxonomy);
?>
<ul class="split-list2">
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
</ul> ?>
And I found a plugin to make thumbnails for categories/taxonomies:
https://wordpress.org/plugins/taxonomy-images/
But I can't figure out how to add the code to include the thumbnails to the taxonomy category grids.
Does anyone know how to do this
Read the plugin doc perfactly it gives what you want
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'after' => '</div>',
'after_image' => '</span>',
'before' => '<div class="my-custom-class-name">',
'before_image' => '<span>',
'image_size' => 'detail',
'post_id' => 1234,
'taxonomy' => 'post_tag',
) );
Second parameter in apply_filters( 'taxonomy-images-get-terms', '' ); is array as show above
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {
print '<ul>';
foreach( (array) $terms as $term ) {
print '<li><a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' ) . '</li>';
}
print '</ul>';
}

Categories