Display custom post categories as plan text - wordpress

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

Related

WordPress taxonomy images plugin displaying all terms

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.

"used for variations" switch programmatically

There are 860+ products with different variations. The task is to allow user bulk edit "used for variations" field for some attributes. For example, we have size, gender that is still needed for variations, but also we have material, that is needed for filters only, so deleting this attribute is not a solution.
Totally untested and not particularly ideal if you have a lot of products as it could time out, but theoretically you could loop through all the variable products and update the _product_attributes meta value. I highly suggest you have a data backup and/or do this is a staging site.
function so_41978670_run_once(){
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'post_type',
'field' => 'slug',
'terms' => 'variable',
),
),
);
$products = new WP_Query( $args );
if( $products->have_posts() ){
while ( $products->have_posts() ) {
$products->the_post();
$product_id = get_the_ID();
$product = wc_get_product( $product_id );
$attributes = $product->get_attributes();
$meta_values = array();
if ( $attributes ) {
foreach ( $attributes as $attribute_key => $attribute ) {
if( in_array( $attribute_key, array( 'size', 'gender' ) ) ){
// Modify the attributes meta.
$attributes[ $attribute_key ]['is_variation'] = 1;
}
}
}
update_post_meta( $product->get_id(), '_product_attributes', $attributes );
}
wp_reset_postdata();
}
}
Potential update for WooCommerce 3.0 CRUD functions, but still untested:
function so_41978670_run_once() {
$args = array(
'limit' => -1,
'type' => 'variable',
);
$products = wc_get_products( $args );
if( ! empty ( $products ) ) {
foreach ( $products as $product ) {
$attributes = $product->get_attributes();
if ( $attributes ) {
foreach ( $attributes as $attribute_key => $attribute ) {
if( in_array( $attribute_key, array( 'size', 'gender' ) ) ){
// Modify the attribute settings.
$attribute->set_variation( true );
}
}
}
$product->set_attributes( $attributes );
$product->save();
}
}
}
add_action( 'admin_init', 'so_41978670_run_once' );

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();

Appending a class attribute to woocommerce single product image

- $image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_link = wp_get_attachment_url( get_post_thumbnail_id() );
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), array(
'title' => $image_title
) );
$attachment_count = count( $product->get_gallery_attachment_ids() );
if ( $attachment_count > 0 ) {
$gallery = '[product-gallery]';
} else {
$gallery = '';
}
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
Above is the code from product-image.php file of woocommerce. I want to add a class to the image of single product. I am unable to find out the way for it. Please help.
Thanks
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), array(
'title' => $image_title,
'class' => 'your class here'
));
I added the class key in get_the_post_thumbnail

Woocommerce, sort dropdown on shortcode based product lists

In our shop, we have a number of standard WP pages. On these pages we show ~40 products using the standard Woocommerce shortcodes.
For example:
[product_category category="boots" per_page="20" columns="4" orderby="price" order="desc"]
The products appears, but there are two things missing:
No sorting dropdown appears above the product lists, so the products cannot be sorted by our visitors.
We don't see any pagination buttons, so it's impossible to see more than the first 20 products on each page.
Any ideas how we can fix those two things?
Regarding your first issue, I have not found any good solutions beside hacking the shortcode within WC. Not entirely advisable as it will be overwritten each upgrade/patch of WC. If it's absolutely necessary, you could maintain the code by rewriting the hack each time you upgrade.
Alright, first download a copy of includes\class-wc-shortcodes.php in your woocommerce folder. Make a backup of the original before editing it, I usually rename the file with a -o or change the file type to .bak.
Assuming you'd want the original sort by dropdown that comes with WC on the Shop Page.
Step 1
Remove orderby and order arguments on the shortcode:
[product_category category="boots" per_page="20" columns="4"]
Step 2
Edit the Product Category shortcode on class-wc-shortcodes.php as such:
/**
* List products in a category shortcode
*
* #access public
* #param array $atts
* #return string
*/
public static function product_category( $atts ) {
global $woocommerce_loop, $wpdb;
if ( empty( $atts ) ) return '';
extract( shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc',
'category' => '',
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), $atts ) );
if ( ! $category ) return '';
// Default ordering args
// $ordering_args = WC()->query->get_catalog_ordering_args( $orderby, $order ); // COMMENT THIS OUT
$orderby = 'title';
$order = 'asc';
if ( isset( $_GET['orderby'] ) ) {
$getorderby = $_GET['orderby'];
}
if ($getorderby == 'popularity') {
$orderby = 'meta_value_num';
$order = 'desc';
$meta_key = 'total_sales';
} elseif ($getorderby == 'rating') {
$fields .= ", AVG( $wpdb->commentmeta.meta_value ) as average_rating ";
$where .= " AND ( $wpdb->commentmeta.meta_key = 'rating' OR $wpdb->commentmeta.meta_key IS null ) ";
$join .= "
LEFT OUTER JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID)
LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)
";
$orderby = "average_rating DESC, $wpdb->posts.post_date DESC";
$groupby = "$wpdb->posts.ID";
} elseif ($getorderby == 'date') {
$orderby = 'date';
$order = 'desc';
} elseif ($getorderby == 'price') {
$orderby = 'meta_value_num';
$order = 'asc';
$meta_key = '_price';
} elseif ($getorderby == 'price-desc') {
$orderby = 'meta_value_num';
$order = 'desc';
$meta_key = '_price';
}
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $orderby, // $ordering_args['orderby'],
'order' => $order, // $ordering_args['order'],
'meta_key' => $meta_key,
'fields' => $fields,
'where' => $where,
'join' => $join,
'groupby' => $groupby,
'posts_per_page' => $per_page,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category ) ),
'field' => 'slug',
'operator' => $operator
)
)
);
if ( isset( $ordering_args['meta_key'] ) ) {
$args['meta_key'] = $ordering_args['meta_key'];
}
ob_start();
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<div style="width:100%;">
<div style="float:right">
<form class="woocommerce-ordering" method="get">
<select name="orderby" class="orderby">
<?php
$catalog_orderby = apply_filters( 'woocommerce_catalog_orderby', array(
'menu_order' => __( 'Default sorting', 'woocommerce' ),
'popularity' => __( 'Sort by popularity', 'woocommerce' ),
'rating' => __( 'Sort by average rating', 'woocommerce' ),
'date' => __( 'Sort by newness', 'woocommerce' ),
'price' => __( 'Sort by price: low to high', 'woocommerce' ),
'price-desc' => __( 'Sort by price: high to low', 'woocommerce' )
) );
if ( get_option( 'woocommerce_enable_review_rating' ) === 'no' )
unset( $catalog_orderby['rating'] );
foreach ( $catalog_orderby as $id => $name )
echo '<option value="' . esc_attr( $id ) . '" ' . selected( $getorderby, $id, false ) . '>' . esc_attr( $name ) . '</option>';
?>
</select>
<?php
// Keep query string vars intact
foreach ( $_GET as $key => $val ) {
if ( 'orderby' === $key || 'submit' === $key )
continue;
if ( is_array( $val ) ) {
foreach( $val as $innerVal ) {
echo '<input type="hidden" name="' . esc_attr( $key ) . '[]" value="' . esc_attr( $innerVal ) . '" />';
}
} else {
echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $val ) . '" />';
}
}
?>
</form>
</div>
</div>
<div style="clear:both;"></div>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
Upload the file back into the includes folder and you're done! Your shortcode product pages will now have a working sort by dropdown same as the one that appears on the WC Shop Page.
Edit the sort options to your liking! Hope it helps!
Well for your second problem: Your shortcode is limiting to see only 20 products. Change the it to per_page="40" and you should see 40 products or simply remove the line and the number of products is not limited.
For your first problem I don't have an answer. I'm looking it for my self as well :).
If you add the paginate="true" attribute to your [products] shortcode, then the shortcode, then the Sort by dropdown menu will appear on the page.
Change this
[product_category category="boots" per_page="20" columns="4"]
to this
[product_category category="boots" paginate="true" limit="20" columns="4"]

Resources