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>
Related
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>';
}
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>';
}
?>
I am displaying several product attributes and custom meta fields on the single product page with this code I got here:
function action_woocommerce_single_product_summary() {
global $product;
$attributes_names = array( 'Brand', 'Color', 'Size' );
$attributes_data = array();
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}
// NOT empty
if ( ! empty($attributes_data) ) {
echo '<h4>' . __( 'Details', 'woocommerce' ) . '</h4><ul><li>' . implode( '</li><li>', $attributes_data );
}
// Get meta
$sn = $product->get_meta( 'Serial_Number' );
$mpn = $product->get_meta( 'MPN' );
// NOT empty
if ( ! empty ( $sn ) ) {
echo '<li>' . __( 'My label 1: ', 'woocommerce' ) . $sn . '</li>';
}
// NOT empty
if ( ! empty ( $mpn ) ) {
echo '<li>' . __( 'My label 2: ', 'woocommerce' ) . $mpn . '</li>';
}
echo '</ul>';
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 36, 0 );
My problem is that the h4 ('Details') and the starting ul are only displayed when there are attributes. When there are only custom fields set for a product, the h4 and the starting ul for the list are not displayed. How can I always show the h4 and the starting whenever either an attribute or a custom field is set for a product?
EDIT to clarify: There are 3 possible product types:
products that have attributes AND custom fields
products that ONLY have an attribute OR custom field
products that neither have an attribute or custom field
For the first type of products, it should be displayed for example:
<h4>Details</h4>
<ul>
<li>Brand: ...</li>
<li>MPN: ...</li>
</ul>
For the second type of products an example would be:
<h4>Details</h4>
<ul>
<li>MPN: ...</li>
</ul>
and for the last type of products NOTHING should be displayed at all.
There are multiple ways to set that up, you could do that using either if/else statement or a ternary operator. For example you could do something like this:
// Using "if/else" method
if ( ! empty($attributes_data) ) {
echo '<h4>' . __( 'Details', 'woocommerce' ) . '</h4><ul><li>' . implode( '</li><li>', $attributes_data );
}else{
// if it's empty you could output a placeholder text OR a any value you could get from woocommerce
echo '<h4>' . __( 'Details', 'woocommerce' ) . '</h4><span>' . {YOUR VALUE OR PLACEHOLDER TEXT AS A FALLBACK} . '</span>';
}
Or you could use the ternary operator like so:
echo ( ! empty($attributes_data) ) ? '<h4>' . __( 'Details', 'woocommerce' ) . '</h4><ul><li>' . implode( '</li><li>', $attributes_data ) : '<h4>' . __( 'Details', 'woocommerce' ) . '</h4><span>' . {YOUR VALUE OR PLACEHOLDER TEXT AS A FALLBACK} . '</span>';
EDIT
Assuming that Serial_Number and MPN are your custom fields, you could set your if condition like this:
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 36, 0 );
function action_woocommerce_single_product_summary()
{
global $product;
$attributes_names = array( 'Brand', 'Color', 'Size' );
$attributes_data = array();
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}
$sn = $product->get_meta( 'Serial_Number' );
$mpn = $product->get_meta( 'MPN' );
// Assuming that 'Serial_Number' and 'MPN' are your custom fields
if(! empty($attributes_data) && ! empty ( $sn ) && ! empty ( $mpn )){
echo '<h4>' . __( 'Details', 'woocommerce' ) . '</h4><ul><li>' . implode( '</li><li>', $attributes_data );
echo '<li>' . __( 'My label 1: ', 'woocommerce' ) . $sn . '</li>';
echo '<li>' . __( 'My label 2: ', 'woocommerce' ) . $mpn . '</li>';
echo '</ul>';
}elseif(! empty($attributes_data) || ! empty ( $sn ) || ! empty ( $mpn )){
echo ( ! empty($attributes_data)) ? '<h4>' . __( 'Details', 'woocommerce' ) . '</h4><ul><li>' . implode( '</li><li>', $attributes_data ): '<h4>' . __( 'Details', 'woocommerce' ) . '</h4><ul>';
echo (! empty ( $sn )) ? '<li>' . __( 'My label 1: ', 'woocommerce' ) . $sn . '</li>' : "";
echo (! empty ( $mpn )) ? '<li>' . __( 'My label 2: ', 'woocommerce' ) . $mpn . '</li>': "";
echo '</ul>';
}else{
echo '<h4>' . __( 'Details', 'woocommerce' ) . '</h4><p>Looks like there is no extra info on this product!</p>';
}
}
Let me know if that is what you're looking for!
I have a little script that display any categories that contain posts and displays them (like a little menu)
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
echo '<a class="blog-panel-cat-menu bg-color-1" href="' . get_permalink( get_option( 'page_for_posts' ) ) . '">All</a>';
foreach( $categories as $category ) {
if ($category->count > 0){
$category_link = sprintf(
'<a class="blog-panel-cat-menu bg-color-1" href="%1$s" alt="%2$s">%3$s</a>',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name )
);
echo $category_link;
}
}
?>
When clicking on any link it will redirect to a page that show the posts for that category eg mywebsite/news/category/blog/ or mywebsite/news/category/news/ (mywebsite/news being the blog homepage which displays all categories)
When I'm on mywebsite/news/category/blog/ I want the "blog" link in the menu to have the class blog-cat-focus after the current class in the foreach loop like this <a class="blog-panel-cat-menu bg-color-1 blog-cat-focus" href="%1$s" alt="%2$s">%3$s</a>
Try this code. Added two more lines to check current category.
<?php
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
$category = get_category( get_query_var( 'cat' ) );
echo $cat_id = $category->cat_ID;
echo '<a class="blog-panel-cat-menu bg-color-1" href="' . get_permalink( get_option( 'page_for_posts' ) ) . '">All</a>';
foreach( $categories as $category ) {
if ($category->count > 0){
$cust_class = '';
if($category->term_id==$cat_id){$cust_class = 'blog-cat-focus';}
$category_link = sprintf(
'<a class="blog-panel-cat-menu bg-color-1 %4$s>" href="%1$s" alt="%2$s">%3$s</a>',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name ),
$cust_class
);
echo $category_link;
}
}
?>
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>';
}