Skip to content

Instantly share code, notes, and snippets.

@pavlo-bondarchuk
Created August 3, 2021 07:11
Show Gist options
  • Save pavlo-bondarchuk/99566f6954c23247170809e4dbfd2712 to your computer and use it in GitHub Desktop.
Save pavlo-bondarchuk/99566f6954c23247170809e4dbfd2712 to your computer and use it in GitHub Desktop.
randomizer_product_category_tag_attribute
add_shortcode( 'randomizer_product_category_tag_attribute', 'randomizer_product_category_tag_attribute_func' );
function randomizer_product_category_tag_attribute_func( ){
$attribute = get_field('attributy', 'attributes');
$taxonomy = array();
$id = array();
if(!empty($attribute)){
foreach ($attribute as $term) {
$id[] = $term->term_id;
$name = $term->name;
$slug = $term->slug;
$taxonomy[] = $term->taxonomy;
}
}
$product_cat_tag_array = [ 'product_cat', 'product_tag' ];
$tax = array_merge($taxonomy,$product_cat_tag_array);
$args = [
'taxonomy' => $tax,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'fields' => 'all',
];
$terms = get_terms( $args );
// Random order
shuffle($terms);
// Get first $max items
$terms = array_slice($terms, 0, 1);
// Sort by name
usort($terms, function($a, $b){
return strcasecmp($a->name, $b->name);
});
// Echo random terms sorted alphabetically
if ($terms) {
foreach($terms as $term) {
return ''.__( 'For example:','porto').' <a href="' .get_term_link( $term, $taxonomy ) . '" title="' . sprintf( __( "View in %s", 'porto' ), $term->name ) . '" ' . '>' . $term->name.'</a>';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment