I'm having issues formatting the breadcrumbs on my staging site. I had them set up with the correct categories before, but the parent category kept displaying the wrong category (which I did fix, but...). The fix caused the "home" option to disappear. I don't know any php and have been trying without success to figure out how to add a "Home /" option on each page before the category. Below is the code I'm using now. Anyone have any ideas?
if ( ! empty( $breadcrumb ) ) {
echo $wrap_before;
if ( is_single() && get_post_type() == 'product' ) {
echo $prepend;
if ( $terms = get_the_terms( $post->ID, 'product_cat' ) ) {
$referer = wp_get_referer();
$printed = array();
foreach( $terms as $term){
if(in_array($term->id, $printed)) continue;
$referer_slug = (strpos($referer, '/'.$term->slug.'/'));
if(!$referer_slug==false){
$printed[] = $term->id;
$category_name = $term->name;
$ancestors = get_ancestors( $term->term_id, 'product_cat' );
$ancestors = array_reverse( $ancestors );
foreach ( $ancestors as $ancestor ) {
$ancestor = get_term( $ancestor, 'product_cat' );
if ( ! is_wp_error( $ancestor ) && $ancestor )
echo $before . '' . $ancestor->name . '' . $after . $delimiter;
}
echo $before . '' . $category_name . '' . $after . $delimiter;
}
}
}
echo $before . get_the_title() . $after;
} else {
foreach ( $breadcrumb as $key => $crumb ) {
echo $before;
if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
echo '' . esc_html( $crumb[0] ) . '';
} else {
echo esc_html( $crumb[0] );
}
echo $after;
if ( sizeof( $breadcrumb ) !== $key + 1 ) {
echo $delimiter;
}
}
}
echo $wrap_after;
}
Thanks for the help in advance!
I think you can achieve this by changing the following line echo $wrap_before; to this:
echo $wrap_before.'Home ยป';
Related
I am new to php. I have these errors appearing on some Wordpress pages.
Warning: count(): Parameter must be an array or an object that implements Countable in /www/tastingvictoria_289/public/wp-content/themes/astra-child/template-parts/content-single.php on line 38
Warning: Invalid argument supplied for foreach() in /www/tastingvictoria_289/public/wp-content/themes/astra-child/template-parts/content-single.php on line 40
This is the related code.
<?php $terms = get_the_terms( $post->ID , 'category' );
$total = count($terms); // 38
$i=0;
foreach ( $terms as $term ) {
if($term->slug != "featured-post"){
$i++;
$term_link = get_term_link( $term, 'category' );
if( is_wp_error( $term_link ) )
continue;
echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
if ($i != $total) echo ' ';
}
}
?>
Any explanation?
As the error message, the $term parameter that you pass into count() function is not countable (in some case - eg, the post id is not exit).
To fix this, please change your code into:
<?php $terms = get_the_terms( $post->ID , 'category' );
if(is_array($terms)){
$total = count($terms); // 38
$i=0;
foreach ( $terms as $term ) {
if($term->slug != "featured-post"){
$i++;
$term_link = get_term_link( $term, 'category' );
if( is_wp_error( $term_link ) )
continue;
echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
if ($i != $total) echo ' ';
}
}
}
?>
Convert the $terms to an array for getting valid result of count($terms).
get_the_terms() returns Array of WP_Term objects on success, false if there are no terms or the post does not exist, WP_Error on failure.
May be the terms not exists. Try following:
$terms = get_the_terms( $post->ID , 'category' );
if ( $terms && ! is_wp_error( $terms ) ) :
$total = count($terms);
$i=0;
foreach ( $terms as $term ) {
if($term->slug != "featured-post"){
$i++;
$term_link = get_term_link( $term, 'category' );
if( is_wp_error( $term_link ) )
continue;
echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
if ($i != $total) echo ' ';
}
}
endif;
Mainly the get_the_terms() function behind code like this
function get_the_terms( $post, $taxonomy ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$terms = get_object_term_cache( $post->ID, $taxonomy );
if ( false === $terms ) {
$terms = wp_get_object_terms( $post->ID, $taxonomy );
if ( ! is_wp_error( $terms ) ) {
$term_ids = wp_list_pluck( $terms, 'term_id' );
wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' );
}
}
/**
* Filters the list of terms attached to the given post.
*
* #since 3.1.0
*
* #param WP_Term[]|WP_Error $terms Array of attached terms, or WP_Error on failure.
* #param int $post_id Post ID.
* #param string $taxonomy Name of the taxonomy.
*/
$terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );
if ( empty( $terms ) ) {
return false;
}
return $terms;
}
In this case, you can see if there is no post then it'll return false, and if the function returns false then your code will be like this:
<?php $terms = get_the_terms( $post->ID , 'category' );
$total = count($terms = false); // if there is no post
$i=0;
foreach ( false as $term ) { //if there is no post
if($term->slug != "featured-post"){
$i++;
$term_link = get_term_link( $term, 'category' );
if( is_wp_error( $term_link ) )
continue;
echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
if ($i != $total) echo ' ';
}
}
?>
Note: the count() function accept an array or object and the foreach() function accept iterable_expression as well. That is why you're getting the warnings.
So, in that case, you can check the return output of the get_the_terms() function like this:
<?php $terms = get_the_terms( $post->ID , 'category' );
if(is_iterable($terms)){
$total = count($terms); // 38
$i=0;
foreach ( $terms as $term ) {
if($term->slug != "featured-post"){
$i++;
$term_link = get_term_link( $term, 'category' );
if( is_wp_error( $term_link ) )
continue;
echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
if ($i != $total) echo ' ';
}
}
}else{
//do something
}
?>
Thank you
I have this code for the tag list in Wordpress posts, where I add an external link next to each tag:
$terms = get_the_tags();
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
echo '<ul>';
foreach ( $terms as $term ) {
$link = get_term_link( $term );
if ( is_wp_error( $link ) ) {
continue;
}
$external_link = 'https://www......../?search3=' . $term->name . '&pn=xxxx';
echo '<li>' .
'' . $term->name . ' ' .
'img' .
'</li>';
}
echo '</ul>';
}
Now I have added a custom field to tags, its slug is "_tag_link". I'd like to replace the actual "$external_link" with the custom field "_tag_link" and show it only if it exists.
I changed the code this way but I don't know how to get "$term_id" (and if the code is correct in general):
$terms = get_the_tags();
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
echo '<ul>';
foreach ( $terms as $term ) {
$link = get_term_link( $term );
if ( is_wp_error( $link ) ) {
continue;
}
echo '<li>' .
'' . $term->name . ' ';
if ( $external_link = get_term_meta($term_id, '_tag_link', true) ) {
echo 'img';
}
echo '</li>';
}
echo '</ul>';
}
Thank you!
You can get term_id from the term object:
$terms = get_the_tags();
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
echo '<ul>';
foreach ( $terms as $term ) {
$link = get_term_link( $term );
if ( is_wp_error( $link ) ) {
continue;
}
echo '<li>' . '' . $term->name . ' ';
if ( $external_link = get_term_meta( $term->term_id, '_tag_link', true ) ) {
echo 'img';
}
echo '</li>';
}
echo '</ul>';
}
I'm using the following function to remove the product title from the breadcrumbs displayed on the product page:
add_filter( 'woocommerce_get_breadcrumb', 'ed_change_breadcrumb' );
function ed_change_breadcrumb( $breadcrumb ) {
if(is_singular()){
array_pop($breadcrumb);
}
return $breadcrumb;
}
It works in that it does remove the title, but it also stops the last category/sub-category from being a hyperlink. How can I fix that?
For example:
Original breadcrumb
<a>Home</a> / <a>Category</a> / <a>Sub Category</a> / Product Title
Result of the above function
<a>Home</a> / <a>Category</a> / Sub Category
I need the Sub Category to still be clickable after removing the product title from the breadcrumbs.
Thanks
Your code works but the last element in the breadcrumbs never contains a link through the code used in global/breadcrumb.php template file on line 34
This template can be overridden by copying it to yourtheme/woocommerce/global/breadcrumb.php.
So you can remove your filter hook and apply the following code in the template file so that it provides a link to the last element when is_product() is true
Note: is_product() - Returns true on a single product page. Wrapper for is_singular()
Replace
if ( ! empty( $breadcrumb ) ) {
echo $wrap_before;
foreach ( $breadcrumb as $key => $crumb ) {
echo $before;
if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
echo '' . esc_html( $crumb[0] ) . '';
} else {
echo esc_html( $crumb[0] );
}
echo $after;
if ( sizeof( $breadcrumb ) !== $key + 1 ) {
echo $delimiter;
}
}
echo $wrap_after;
}
With
if ( ! empty( $breadcrumb ) ) {
echo $wrap_before;
foreach ( $breadcrumb as $key => $crumb ) {
echo $before;
if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
echo '' . esc_html( $crumb[0] ) . '';
} else {
if ( is_product() ) {
unset($crumb);
} else {
echo esc_html( $crumb[0] );
}
}
echo $after;
if ( sizeof( $breadcrumb ) !== $key + 1 ) {
if ( is_product() && sizeof( $breadcrumb ) == $key + 2 ) {
echo '';
} else {
echo $delimiter;
}
}
}
echo $wrap_after;
}
I have a page like this:
I have manage stocks enabled in variations, and I want to filter out items that are not in stock. For example, if size 9.5 is not available in variations, it should not appear in the front end as well. The questions posted here are all quite old and I could not get any of the suggestions posted in them to work. Here is what I have done so far:
// Get terms if this is a taxonomy - ordered
if ( taxonomy_exists( $attribute_name ) ) {
$is_attr_color = false;
$attribute_color = wc_sanitize_taxonomy_name( 'color' );
if( $attribute_name == wc_attribute_taxonomy_name( $attribute_color ) ){
$is_attr_color = true;
}
$terms = wc_get_product_terms( $post->ID, $attribute_name, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) ) {
continue;
}
if( $is_attr_color ){
$datas = get_term_meta( $term->term_id, 'ts_product_color_config', true );
if( strlen( $datas ) > 0 ){
$datas = unserialize( $datas );
}else{
$datas = array(
'ts_color_color' => "#ffffff"
,'ts_color_image' => 0
);
}
}
$class = sanitize_title( $selected_value ) == sanitize_title( $term->slug ) ? 'selected' : '';
$class .= ' option';
if( $is_attr_color ){
$class .= ' color';
}
echo '<div data-value="' . esc_attr( $term->slug ) . '" class="' . $class . '">';
if( $is_attr_color ){
if( absint($datas['ts_color_image']) > 0 ){
echo '' . wp_get_attachment_image( absint($datas['ts_color_image']), 'ts_prod_color_thumb', true, array('title'=>$term->name, 'alt'=>$term->name) ) . '';
}
else{
echo '' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '';
}
}
else{
// !!!!!!!!!!MAIN ISSUE HERE!!!!!!!!!!!!
if($term->count > 0){
echo '' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '';
}
}
echo '</div>';
}
} else {
foreach ( $options as $option ) {
$class = sanitize_title( $selected_value ) == sanitize_title( $option ) ? 'selected' : '';
$class .= ' option';
echo '<div data-value="' . esc_attr( $option ) . '" class="' . $class . '">' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</div>';
}
}
Any idea how I could fix this? I have been at it for hours and I cannot find a solution for it. It's really frustrating.
I am trying to add Publisher, Topic and Author to a Single Product with help of categories/subcategories. This is how it looks after hours of coding/and copying (very fresh with WooCommerce tbh)
This is what I am getting, but it shows ALL subcategories, not only the ones associated to the Product, this is the code I am using
function get_product_subcategories_list( $category_slug ){
$terms_html = array();
$taxonomy = 'product_cat';
// Get the product category (parent) WP_Term object
$parent = get_term_by( 'slug', $category_slug, $taxonomy );
// Get an array of the subcategories IDs (children IDs)
$children_ids = get_term_children( $parent->term_id, $taxonomy );
// Loop through each children IDs
foreach($children_ids as $children_id){
$term = get_term( $children_id, $taxonomy ); // WP_Term object
$term_link = get_term_link( $term, $taxonomy ); // The term link
if ( is_wp_error( $term_link ) ) $term_link = '';
// Set in an array the html formated subcategory name/link
$terms_html[] = '' . $term->name . '';
}
return '<span class="subcategories-' . $category_slug . '">' . implode( ', ', $terms_html ) . '</span>';
}
add_action('woocommerce_single_product_summary','monolith_cat_scan', 31);
function monolith_cat_scan() {
echo '<p>Topic : ';
echo get_product_subcategories_list( 'topics' );
echo '</p>';
echo '<p>Publisher : ';
echo get_product_subcategories_list( 'publishers' );
echo '</p>';
echo '<p>Author: ';
echo get_product_subcategories_list( 'authors' );
echo '</p>';
}
But I can't get the whole thing to work like I want to and get the subcategories of the Single Product, in this example only Spirituality, SOUNDS TRUE INC (only sub cat in Publishers), and Allan Watts should be there.
I'd appreciate every help!
I got a working code (it doesn't look beautiful I know but it's the best I could do and it does the trick.
add_action('woocommerce_single_product_summary','monolith_cat_scan', 31);
function monolith_cat_scan() {
global $post;
$cats = get_the_terms( $post->ID, 'product_cat' );
if ( ! empty( $cats ) ) {
foreach ( $cats as $term ) {
if( $term->parent == 30 ) {
echo '<p>Topic : ' . $term->name . '';
}
}
}
}
add_action('woocommerce_single_product_summary','monolith_cat_scan2', 32);
function monolith_cat_scan2() {
global $post;
$cats = get_the_terms( $post->ID, 'product_cat' );
if ( ! empty( $cats ) ) {
foreach ( $cats as $term ) {
if( $term->parent == 31 ) {
echo '<p>Author : ' . $term->name . '';
}
}
}
}
add_action('woocommerce_single_product_summary','monolith_cat_scan3', 33);
function monolith_cat_scan3() {
global $post;
$cats = get_the_terms( $post->ID, 'product_cat' );
if ( ! empty( $cats ) ) {
foreach ( $cats as $term ) {
// If parent cat ID = 116 echo subcat name...
if( $term->parent == 32 ) {
echo '<p>Publisher : ' . $term->name . '';
}
}
}
}