get_terms with Redis Object Cache question - php

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

Related

How Can I get term/ category name wordpress

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

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>

Sorting wordpress posts by date published

im using the following code to display my posts on a page in wordpress that use a featured image:
$mypages = get_pages( array() );
if ( !empty( $mypages ) ) {
echo '<ul>';
foreach ( $mypages as $mypage ) {
if ( get_the_post_thumbnail( $mypage->ID ) ) {
echo '<div class="featured-container">';
echo '<div class="featured-image">';
echo '<li><a class="feat-hover" href="' . get_permalink( $mypage->ID ) . '">' . get_the_post_thumbnail( $mypage->ID ) . '</a></li>';
echo '</div>';
echo '<div class="featured-text">';
echo '' . get_the_title($mypage->ID ) . '';
echo '</div>';
echo '</div>';
}
}
echo '</ul>';
}
but before printing this information out i want to sort the $mypages array so that they display by date published. ive tried this code:
<?php $args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date',
);
$mypages = get_pages($args);
?>
but it doesnt seem to work, am i missing something or doing this the wrong way?
thanks in advance.
FULL CODE BEING USED:
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date'
);
$mypages = get_pages( array($args) );
if ( !empty( $mypages ) ) {
echo '<ul>';
foreach ( $mypages as $mypage ) {
if ( get_the_post_thumbnail( $mypage->ID ) ) {
echo '<div class="featured-container">';
echo '<div class="featured-image">';
echo '<li><a class="feat-hover" href="' . get_permalink( $mypage->ID ) . '">' . get_the_post_thumbnail( $mypage->ID ) . '</a></li>';
echo '</div>';
echo '<div class="featured-text">';
echo '' . get_the_title($mypage->ID ) . '';
echo '</div>';
echo '</div>';
}
}
echo '</ul>';
}
The issue is that you're passing $args into another array(). You only need to pass in the $args directly, since it's already an array(). Change the top block of code to:
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date'
);
$mypages = get_pages( $args );

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

wordpress sub taxonomies are not showing properly

I want to display only 1st level of sub taxonomies, below is my code,it's just not working, can anyone please tell whats wrong with my code?? Many thanks.
<?php
$term_id = 31;
$taxonomy_name = 'kosher_category';
$args = array('child_of' => $term_id, 'parent' => $term_id);
$termchildren = get_terms($taxonomy_name, $args );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>
Add hide_empty.
<?php
$term_id = 31;
$taxonomy_name = 'kosher_category';
$args = array(
'child_of' => $term_id,
'parent' => $term_id,
'hide_empty' => false // ADDED
);
$termchildren = get_terms($taxonomy_name, $args );
echo '<ul>';
foreach($termchildren AS $child) {
$term = get_term_by('id', $child, $taxonomy_name);
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>

Categories