I have this code in my WP theme which shows categories in breadcrumbs of products.
The problem is when product has more than 5 categories - it looks ugly.
My question is: How to show ONLY 5 categories?
Thanks!
<?php
$term_list = '';
$j=0;
foreach ($terms as $term) {
if($term->parent==0){
$j++;
if( $j <= 1 ){
$term_list .= '' . $term->name . '';
}
}
}
if(strlen($term_list) > 0){
$size = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
echo $product->get_categories( ', ', '<div class="nav-back">' . _n( 'Back to','Back to', $size, 'tdl_framework' ) . '', '</div>' );
};
?>
Add
$terms = array_slice( $terms, 0, 5 );
just before the foreach to trim it to 5 elements max.
Related
I like to list a specific WooCommerce attribute values as list items like this:
<ul>
<li>Attribute value 1<li>
<li>Attribute value 2<li>
</ul>
I have this code:
global $product;
if ( $product ) {
$attributes = array( 'gyogynovenyek' );
$output = array();
foreach ( $attributes as $attribute ) {
$taxonomy = 'pa_' . $attribute;
$values = $product->get_attribute($taxonomy);
if ( ! empty($values) ) {
$output = '<li>' . $values . '</li>';
}
}
echo $output;
}
the problem, it lists the values like this:
<li>Attribute value 1, Attribute value 2</li>
How should I modify the code to list values separately? Thanks for your answers!
Okay, I find the answer:
$terms = get_terms( 'pa_gyogynovenyek' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
I need to display on my single page, each taxonomy name and image.
i have 10 different image for taxonomy : 'brique'
It's ok for the name but i can't display image
For the image
<?php
$tax = 'brique';
$terms = get_terms( $tax, $args = array(
'hide_empty' => false, // do not hide empty terms
));
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field('visuel' . $term_id );
if( $term->count > 0 ) {
echo '<a href="' . esc_url( $term_link ) . '">';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
echo $term->name .'</a>';
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
?>
Try the below code. first, you need to pass taxonomy Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array: you can check here get_terms()
in get_field you have pass second parameter as term_id or $term object
<?php
$tax = 'brique';
$terms = get_terms( array(
'taxonomy' => $tax,
'hide_empty' => false,
) );
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field( 'visuel', $term );
if( $term->count > 0 ) {
echo '<a href="' . esc_url( $term_link ) . '">';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
echo $term->name .'</a>';
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
?>
Help
Code in template:
<?php
$groupID = '';
$fields = get_fields($groupID);
$fields = get_field_objects();
if( $fields )
{
foreach( $fields as $field_name => $field )
{
if( $field['value'] )
{
echo '<ul>';
echo '<li>' . $field['label'] . ': <strong>' . $field['value'] . '</strong></li>';
echo '</ul>';
}
}
}
?>
I need to hide the fields:
field_5c0a8d44cf56e
field_5c0a8d4ecf56f
How can i do this?
Your question is not much clear for me, and from my knowledge what I have understood,
for an acf group you don't want to loop through, the control is yours,
so you can print it straightaway
but if you really want to do it in a loop then,
if( $fields )
{
foreach( $fields as $field_name => $value )
{
if( $value && !in_array($field_name, ["field_5c0a8d44cf56e", "field_5c0a8d4ecf56f"])
{
echo '<ul>';
echo '<li>' . $field_name . ': <strong>' . $value . '</strong></li>';
echo '</ul>';
}
}
}
I want to get existing terms by words.
For example: all terms on "products" containing the word "good" will be shown.
So we get: good books, good movies, good toys etc.
Look at this code:
<?php
$args = array( 'hide_empty=0' );
$terms = get_terms( 'products', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$count = count( $terms );
$i = 0;
$term_list = '<p class="my_term-archive">';
foreach ( $terms as $term ) {
$i++;
$term_list .= '' . $term->name . '';
if ( $count != $i ) {
$term_list .= ' · ';
}
else {
$term_list .= '</p>';
}
}
echo $term_list;
}
?>
I took it from https://codex.wordpress.org/Function_Reference/get_terms
This code show me ALL the terms from "products", but I want only the terms from "products" containing the word "good".
Once you have your complete array of taxonomy terms, you can filter out terms containing the word "good" using PHP's preg_grep() function.
$terms = get_terms( 'products', $args );
// This will return all terms that contain "good" in some form, as in 'good books' or 'goodness'
$goodterms = preg_grep("/good/", $array);
I was wondering if you could help me display a taxonomies child links, for example, I have a custom post type of "courses" with a custom taxonomy of "study_type" and categories like "security", "professional" etc. Currently I am correctly displaying the category title and description. I need to know how to get a permalink for the courses that fall into the categories and display them underneath the category description. Thanks for any help or advice you have to offer. Here is me code:
<?php
//list terms in taxonomy
$types[0] = 'study_type';
foreach ($types as $type) {
$taxonomy = $type;
$terms = get_terms( $taxonomy, '' );
if ($terms) {
foreach($terms as $term) {
echo '<h2>'.$term->name.'</h2>';
echo '<p>' . $term->description . '</p>';
//This is where the links should be
}
}
}
?>
use get_term_children
<?php
//list terms in taxonomy
$types[0] = 'study_type';
foreach ($types as $type) {
$terms = get_terms( $type, '' );
if ($terms) {
foreach($terms as $term) {
echo '<h2>'.$term->name.'</h2>';
echo '<p>' . $term->description . '</p>';
//This is where the links should be
$termchildren = get_term_children( $term->term_id, $type );
if($termchildren){
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
}
}
} ?>