Exclude Terms from custom Taxonomy? - wordpress

I have a custom post type in which I have custom taxonomies setup.
I want to print out the categories(custom taxonomy) that a post is included in, but exclude one. I cannot find a solution to exclude the category though.
Here is my code to output a list of the categories the custom post type is filed under:
<?php the_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ' ); ?>
How do I exclude a specific category?
Thanks.

You could create a function in your functions.php file that calls get_the_terms to return the list of terms as an array and then remove the item you don't want.
Give this a try:
function get_excluded_terms( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
if(!in_array($term->term_id,$exclude)) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
$excluded_terms[] = '' . $term->name . '';
}
}
$excluded_terms = apply_filters( "term_links-$taxonomy", $excluded_terms );
return $before . join( $sep, $excluded_terms ) . $after;
}
and then use it like this:
<?php echo get_excluded_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ', array(667)); ?>

Related

Shortcode that display a single product attribute for a WooCommerce product

I am looking for a way to display a specific attribute on the single Product page of WooCommerce.
I found this code in the folling topic; Shortcode that display all product attributes set for a WooCommerce product
function get_product_attributes_shortcode($atts ) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$html = []; // Initializing
foreach ( $product->get_attributes() as $attribute => $values ) {
$attribute_name = wc_attribute_label($values->get_name());
$attribute_data = $values->get_data();
$is_taxonomy = $attribute_data['is_taxonomy'];
$option_values = array(); // Initializing
// For taxonomy product attribute values
if( $is_taxonomy ) {
$terms = $values->get_terms(); // Get attribute WP_Terms
// Loop through attribute WP_Term(s)
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $attribute );
$option_values[] = ''.$term->name.'';
}
}
// For "custom" product attributes values
else {
// Loop through attribute option values
foreach ( $values->get_options() as $term_name ) {
$option_values[] = $term_name;
}
}
$html[] = '<strong>' . $attribute_name . '</strong>: ' . implode(', ', $option_values);
}
return '<div class="product-attributes">' . implode(' | ', $html) . '<div>';
}
}
add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );
I can display all attributes by using the shortcode [display-attributes] but I want to display only one specific attribute (in my case the attribute is "Kunstenaar"). However I don't know how to adjust the code to make this work. Anyone who can help me out?
If you need just specific attribute, you don't have to get them all and then go through them. WC have function get_attribute() - documentation
function get_product_attributes_shortcode( $atts ) {
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$kunstenaar = $product->get_attribute( 'Kunstenaar' );
return '<div class="product-attributes"><strong>Kunstenaar</strong>: ' . $kunstenaar . '<div>';
}
}
add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );

Custom taxonomies won't display alphabetically

Is there anyone who knows why this is going on. Wordpress lists my custom taxonomies in the order they where inserted and not alphabetically. This is the code I'm using.
function get_the_term_list_inclusief( $id, $taxonomy, $before = '', $sep = '', $after = '') {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
$links = array();
foreach ( $terms as $term ) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) ) {
return $link;
}
$links[] = '<a class="ajax" href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
}
/**
* Filter the term links for a given taxonomy.
*
* The dynamic portion of the filter name, `$taxonomy`, refers
* to the taxonomy slug.
*
* #since 2.5.0
*
* #param array $links An array of term links.
*/
$term_links = apply_filters( "term_links-$taxonomy", $links );
return $before . join( $sep, $term_links ) . $after;
}
Is there away to use this code and have the taxonomies order by title or ASC?
Thanks
Try if this works:
// ...
$links = array();
usort($terms, function($a, $b) {
return strcmp($a->name, $b->name);
});
foreach ( $terms as $term ) {
// ...

Remove link to taxonomy terms $post->ID

how to remove the link on the categories?
this is my code
$taxonomy = 'casestudies_category';
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
$separator = ', ';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
echo $terms;
}
wp_list_categories displays a list of categories as links.
If you want to get just list of categories - use get_categories()
Or describe what you want to get in result.

Wordpress: Display Subcategory Name Without Link

I have this code that works great to show the subcategory name and link of a specific parent category of my choosing:
<?php
$taxonomy = 'category';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$categories = get_the_category();
$parentid = '6';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = strtr( wp_list_categories( 'title_li=&style=none&echo=0&child_of=' . $parentid . '&taxonomy=' . $taxonomy . '&include=' . $term_ids), array( '<br />' => ' <br /> ' ) );
echo preg_replace( '#\s<br />\s\n$#', '', $terms );
}
?>
Now I want to be able to do the same thing but WITHOUT the code above generating the link automatically. Any ideas?
Use get_categories() instead of wp_list_categories()
$terms = get_categories("child_of={$parentid}&include={$term_ids}");
foreach($terms as $term)
echo $term->name;

get post types by taxonomies in wordpress

How to get all the post types(not the posts) grouped by taxonomies?
Is there any standard wordpress functions?
I want something like this
taxonomy_1 -> post_type_11, post_type_12, post_type_13, ....
taxonomy_2 -> post_type_21, post_type_22, ....
I have written a query to get the taxonomies and the attached post types as arrays
$query = "
SELECT taxonomy, GROUP_CONCAT( DISTINCT `post_type` SEPARATOR ',') AS `post_types`
FROM wp_term_taxonomy
JOIN wp_term_relationships ON wp_term_relationships.`term_taxonomy_id` = wp_term_taxonomy.`term_taxonomy_id`
JOIN wp_posts ON wp_term_relationships.`object_id` = wp_posts.`ID`
/* you can add other conditions here like - AND post_status = 'published' */
GROUP BY taxonomy
";
then I call this query
$global wpdb;
$post_types_by_taxonomies = $wpdb->get_results( $query, OBJECT_K );
now we can loop through the result array
foreach($post_types_by_taxonomies as $taxonomy => $post_types_as_string){
$post_types = explode(',', $post_types_as_string->post_types);
echo '<hr />';
echo $taxonomy;
echo '<br />';
echo '<br />';
foreach($post_types as $post_type){
echo $post_type;
echo '<br />';
}
}
Please check this one i hope this will work for you.
<?php
// get taxonomies terms links
function custom_taxonomies_terms_links(){
// get post by post id
$post = get_post( $post->ID );
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$out = array();
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( !empty( $terms ) ) {
$out[] = "<h2>" . $taxonomy->label . "</h2>\n<ul>";
foreach ( $terms as $term ) {
$out[] =
' <li><a href="'
. get_term_link( $term->slug, $taxonomy_slug ) .'">'
. $term->name
. "</a></li>\n";
}
$out[] = "</ul>\n";
}
}
return implode('', $out );
}
?>

Resources