WordPress taxonomy images plugin displaying all terms - wordpress

I am using taxonomy-images/ WordPress plugin (https://en-gb.wordpress.org/plugins/taxonomy-images/)
Target: I have got a poster taxonomy and I would like to display term name and term image. I would like to be able to display, retrieve ALL terms in my taxonomy, no matter term name is empty.
Issue: But if both data are not entered I can not display the term. I am not managing to correctly make use of 'hide_empty'.
Any help appreciated.
Thanks
<?php
/*
Template Name: gof Poster Home Page */
// https://en-gb.wordpress.org/plugins/taxonomy-images/
?>
<?php
$taxonomy = 'month-category';
$orderby = 'name';
$order = 'ASC';
$show_count = false;
$pad_counts = false;
$hierarchical = true;
$hide_empty = false;
$title = '';
$images = 'image_id';
$args = array(
'taxonomy' = $taxonomy,
'orderby' = $orderby,
'order' = $order,
'show_count' = $show_count,
'pad_counts' = $pad_counts,
'hierarchical' = $hierarchical,
'hide_empty' = $hide_empty,
'title_li' = $title
);
//$terms = get_terms( 'month-category', $args );
// $terms = apply_filters( 'taxonomy-images-get-terms', 'month-category', $args);
$terms = apply_filters( 'taxonomy-images-get-terms', '', $args);
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$count = count( $terms );
$i = 0;
$term_list = '<div id="poster-cat-wrapper">';
foreach ( $terms as $term ) {
$term_list .=
'<div class="poster-cat">' .
'<a href="/posters/?gof=' . $term->name . '">' .
wp_get_attachment_image( $term->$images, 'detail' ) .
'<p>' . $term->name . '</p>' .
'</a>' .
'</div>';
$i++;
// '' . $term->name . ''
if ( $count != $i ) {
$term_list .= ' ';
}
else {
$term_list .= '</div>';
}
}
}
?>

I cannot figure out how to do this either, but I found a nasty hack. Look for this part in 'public-filters.php' in '/plugins/taxonomy-images/':
$args = wp_parse_args( $args, array(
'cache_images' => true,
'having_images' => true,
'taxonomy' => 'category',
'term_args' => array('hide_empty' => false),
) );
Change it to this:
$args = wp_parse_args( $args, array(
'cache_images' => true,
'having_images' => true,
'taxonomy' => 'category',
'term_args' => array('hide_empty' => false),
) );
Once again: Know that this is NOT the right solution! You should write a filter that does this for you. Every plugin update will break this functionality.

Related

How to exclude specific category and show only one from the get_the_category();

For example i have a post in category: cat1,cat2,cat3 and i want to exclude cat1 and show only one from cat2 or cat3.
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
if($category->name !== 'Cat1'){
$output .= $category->cat_name;}
}
echo trim($output, $separator);
}
?>
I have tried this loop but it only works for excluding "Cat1" i also want to show one category from get_the_category(); ?
Can someone help me?
exclude category by using "exclude" argument with comma separated value category id in wordpress
<?php
$cat_args = array(
'type' => 'post',
'parent' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => true,
'exclude' => '1,2,3',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => true
);
$categories = get_categories( $cat_args );
if(count($categories )>0)
{
$separator = ' ';
$output = '';
foreach ( $categories as $category ) {
$output .= $category->cat_name;
echo trim($output, $separator);
}
}
?>
Something like this
foreach($categories as $category) {
if($category->name !== 'Cat1'){
$output = $category->cat_name;
if($output != '') break; //end foreach when we have value
}

Display custom post categories as plan text

I am using this code to display custom post categories hierarchicaly, everything is working till now great but what I want to achieve also is to display these categories as plain text (without <a href="..."). Can anyone help?
$taxonomy = 'produkte_kategorie'; // change this to your taxonomy
$terms = wp_get_post_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
if( $terms ) {
echo '<ul class="p-kategorie">';
$terms = trim( implode( ',', (array) $terms ), ' ,' );
wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms );
echo '</ul>';
}
You can do that via the following function,
$terms = trim( implode( ',', (array) $terms ), ' ,' );
$categories = get_categories(array('include'=>$terms,'taxonomy'=>'produkte_kategorie'));
foreach ($categories as $category) {
echo $category->cat_name;
}
Actually this didn't worked for me but I made a solution with this code from wordpress codex: https://developer.wordpress.org/reference/functions/wp_list_categories/
$taxonomy = 'category';
// Get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// Separator between links.
$separator = ', ';
if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( array(
'title_li' => '',
'style' => 'none',
'echo' => false,
'taxonomy' => $taxonomy,
'include' => $term_ids
) );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// Display post categories.
echo $terms;
Thanks a lot for your Feedback

woocommerce Recent X commented products

I'am looking for code in Woocommerce that allow me to display x items wth recent comments (reviews) added
simlar to [recent_products per_page="16" columns="4" orderby="rand"] shortcode but allowing to get post only with comments
I don't believe there is a shortcode for this. You'd have to write some PHP. The way that you can do this is:
$number = 5; // Change as desired
$comments = get_comments( array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product' ) );
if ( $comments ) {
echo '<ul class="product_list_widget">';
foreach ( (array) $comments as $comment ) {
$_product = wc_get_product( $comment->comment_post_ID );
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$rating_html = $_product->get_rating_html( $rating );
echo '<li><a href="' . esc_url( get_comment_link( $comment->comment_ID ) ) . '">';
echo $_product->get_image();
echo $_product->get_title() . '</a>';
echo $rating_html;
printf( '<span class="reviewer">' . _x( 'by %1$s', 'by comment author', 'woocommerce' ) . '</span>', get_comment_author() );
echo '</li>';
}
echo '</ul>';
}
(Note: this code is lifted with modification from the WooCommerce plugin, "recent reviews" widget: woocommerce/includes/widgets/class-wc-widget-recent-reviews.php)
This is untested, but something like this should work:
$args = array(
'post_type' => 'product',
'orderby' => 'comment_count',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
if ( $post->comment_count < 1 ) continue;
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();

Display posts of a Sub Category of my taxonomy

I created a Shortcode to display Term of my Taxonomy. But for one of my taxonomy I have a sub term or sub category. And I don't understand how can I display posts of a Sub Category of my Taxonomy.
My Code in functions.php
function theme_lasts_posts_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
"posttype" => '',
"taxonomy" => '',
"term" => '',
"class" => '',
"exclude" => '',
), $atts));
$output = '<div class="derniersarticles">';
if ( $posttype != '' ) {
$loop = new WP_Query( array( 'post_type' => $posttype, 'taxonomy' => $taxonomy,'term' => $term, 'posts_per_page' => 100, 'post__not_in' => array($exclude) ) );
} else {
$loop = new WP_Query( array( 'post_type' => $posttype, 'posts_per_page' => 100 ) );
}
while ( $loop->have_posts() ) : $loop->the_post();
$output .= '<div class="'. $class .'">';
$output .= '<div class="thumb">';
$output .= '' . get_the_post_thumbnail() . '';
$output .= '</div>';
$output .= '<h3>' . get_the_title() . '</h3>';
$output .= '</div>';
endwhile;
$output .= '</div>';
return $output;
}
add_shortcode( 'DerniersArticles', 'theme_lasts_posts_shortcode' );
IF i understood question corectly you need to get posts from children of parent category from custom taxonomy?
In That case, try this :
<?php
$father = get_term_by('name', 'main_category_name', 'product_cat' );
$father_id = $father->term_id;
$taxonomy_name = 'product_cat';
$children = get_term_children( $father_id, $taxonomy_name );
echo '<div class="sidebar_cat">';
echo '<h5 class="sidebar_heading">'.'main_category_name'.'</h5>';
echo '<ul class="sidebar_text">';
foreach ( $children as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>'.$term->name . '</li>';
}
echo '</ul>'; ?>

Wordpress native gallery - own output - sorting difficulty

as you can read, I'm having trouble sorting the gallery in the order I want it to. I'm trying to have it sorted just like in the Drag&Drop Interface, where you edit your gallery. That's the same order as in the id attribute in the shortcode. I just can't figure out what value to assign to $orderby. i tried 'ID', 'menu_order' and 'post__in', but no changes.
Do you have any advice.
add_filter('post_gallery', 'fgf_gallery', 10, 2);
function fgf_gallery($output, $attr) {
global $post;
static $instance = 0;
$instance++;
$id = $post->ID;
$order = 'ASC';
$orderby = 'ID';
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
if ( empty($attachments) ) return;
$output = "<ul id='gallery-{$instance}' class='gallery'>";
foreach ( $attachments as $id => $attachment ) {
$output .= "<li class='gallery-item'>";
$output .= "<img src='".$thumb_src."'>";
$output .= "<h1>".$attachment->post_title."</h1>";
if ( trim($attachment->post_excerpt) ) {
$output .= "
<p class='wp-caption-text gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
<p>";
}
$output .= "</li>";
}
$output .= "</ul>\n";
return $output;
}
Thanks. I appreciated any hint to further documentation as well.
I found what I needed in the wp-includes/media.php file
you get the order from your shortcode with "post__in".
function fgf_gallery_2($output, $attr) {
static $instance = 0;
$instance++;
$order = 'ASC';
$orderby = 'post__in';
$include = $attr['ids'];
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
if ( empty($attachments) ) return;
$output = "<ul id='gallery-{$instance}' class='gallery'>";
foreach ( $attachments as $id => $attachment ) {
/* do what you want here */
}
$output .= "</ul>\n";
return $output;
}
works for me.

Resources