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.
Related
I need to display all the product tags as a dropdown on main page. I tried the following code but it did not work.
$terms = get_terms( 'product_tag' );
$term_array = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$term_array[] = $term->name;
}
}
It always returns an empty array. Any suggestions? Thanks.
I'm not certain what you mean by dropdown, but will use <select> for this answer.
In your functions.php
function get_some_tags_man(){
$terms = get_terms( array(
'hide_empty' => false, // only if you want to hide false
'taxonomy' => 'product_tag',
)
);
$html = '';
if($terms){
$html .= '<select name="terms" id="someID">';
foreach($terms as $term){
$html .= "<option name='$term->name'>$term->name</option>";
}
$html .= '</select>';
}
return $html;
}
in your theme file:
<?php echo get_some_tags_man(); ?>
Try this, it will also show empty tags.
$terms = get_terms( array( 'taxonomy' => 'product_tag', 'hide_empty' => false ) );
I just tried the code (in the product page and Shop page) and it worked.
Main Page you mean Shop Page? The first page that appear when you enter the site.
The theme i'm using is StoreFront.
Can you show the hook you are using? or the code? Look. This is the complete code I use.
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_product_loop_tags', 15 );
function woocommerce_product_loop_tags() {
global $post, $product;
$tag_count = sizeof( get_the_terms( $post->ID, 'product_tag' ) );
$idProducto=$product->id;
$product->get_title();
echo $product->get_tags( ', ', '<span class="tagged">' . _n( 'Tag:', 'Tags:', $tag_count, 'woocommerce' ) . ' ', '.</span>' );
$terms = get_terms( 'product_tag' );
$term_array = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo " ".$term_array[] = $term->name;
}
}
}
In WooCommerce under Products > Attributes > [Name of Attribute] > Add New [Attribute Variation] there is a section titled "Description" with the text "The description is not prominent by default; however, some themes may show it."
I'd like to show it in my theme directly below the attribute variation in the Additional Information tab. this is the code I currently have there. I'd appreciate any advice on how to get the attribute variation description to show just below the attribute variation.
<td><?php
if ( $attribute['is_taxonomy'] ) {
$values = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
} else {
// Convert pipes to commas and display values
$values = array_map( 'trim', explode( WC_DELIMITER, $attribute['value'] ) );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
}
?></td>
See term_description()
Something like the following should create a list of terms with their descriptions:
$values = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'all' ) );
if( $values ){
echo '<dl>';
foreach ( $values as $term ){
echo '<dh>' . $term->name.' </dh>';
echo '<dd>' . term_description( $term->term_id ) . '</dd>';
}
echo '</dl>';
}
Or if you don't want the description running through WordPress' default filters you should be able to just use $term->description.
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;
I'm using woocommerce on my theme, and it comes with a product_cat term as a default category on the shop product. I need to show on every product_cat archive its direct child and the sub-tems, but not the sub terms child.
I tried:
<?php $thispage = $wp_query->post; wp_list_categories("taxonomy=product_cat&term=". $term->slug."&title_li=&child_of=".$thispage->slug);?>
and it returned all product_cat as ul and the sub-term of the archive im in, but not the archive's children.
I tried:
global $post;
$terms = get_the_terms( $post->ID, 'product_cat');
foreach ( $terms as $term )
$currentID = get_the_ID();
$args=array(
'taxonomy'=>'product_cat',
'term' => $term->slug,
'child_of'=>$currentID
);
$my_query = new WP_Query( $args );
?>
<?php if ( $my_query->have_posts() ): ?>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<li> <?php the_title();?></li>
<?php endwhile; ?>
<?php endif; ?>
and it returned all the the current archive's children and the sub-terms children without the sub-term it self.
So I tried :
$term_id = $terms;
$taxonomy_name = 'product_cat';
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
and it returned nothing.
Got an idea... if this returns only the sub term:
wp_list_categories( array('child_of' => get_queried_object_id(),'taxonomy' => 'product_cat','title_li' => '','depth' => 1, 'show_option_none'=> ''));
maybe i can exacude all of its children in a query args and then call the wp_list_categories. tried:
$terms = get_the_terms( $post->ID, 'product_cat'); foreach ( $terms as $term ) $currentID = get_the_ID(); $args=array( 'taxonomy'=>'product_cat', 'term' => $term->slug, 'exclude '=> array('child_of' => get_queried_object_id()) ); $my_query = new WP_Query( $args );?>
but it returns only the sub-term children, not the the proudct_cat children...
Anyone?
I'm Speechless. anyone have a clue?
*sorry for my poor English
I think you could do something like
<?php
$taxonomy = 'product_cat';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $product->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( 'title_li=&style=none&echo=0&child_of=' .get_queried_object_id() . '&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// display post categories
echo $terms;
}
?>
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)); ?>