Figured this would be pretty easy but I am running into an issue.
The website I am building, the client has a list of taxonomies that have an ACF Image Field and ACF Description field.
What they want to do is have a block where they can select certain ingredients from the Taxonomy Block, then have it render out formatted (on a page)(At this time it doesnt need to link to the actual category) but they want to do it this way so they dont need to update page by page when an ingredient changes description or image they can just change it in the taxonomy list.
Below is the code i am using to try and get it going from the docs, it wont render the name or original description it will render the slug but skips over the name but the slug is correct
I've been trying this with no luck, it just returns 3 li's which is correct but i can get a name or custom field to come through.
If i just the the_field('ingredients_selector'); I get the ID's just fine But for the life of me i can not get a term name or the ACF field attached to it/
$terms = get_field('ingredients_selector');
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul class="ingredients-list">';
foreach ( $terms as $term ) {
echo '<li class="ingredients-list__item">' . $term->name . '</li>'; ?>
<p>Description: <?php the_field('description', $term); ?></p>
<p>Image: <?php the_field('image', $term); ?></p>
<?php }
echo '</ul>';
}
?>
I've also tried this way, this gives me same reuslt but the slug with work, it will skip term name again but "view all" will link at least
<?php
$terms = get_field('ingredients_selector');
if( $terms ): ?>
<ul>
<?php foreach( $terms as $term ): ?>
<li>
<h2><?php echo esc_html( $term->name ); ?></h2>
<p>Term description: <?php the_field('description', $term); ?></p>
<p>Term Image: <?php the_field('image', $term); ?></p>
<p><?php echo esc_html( $term->description ); ?></p>
View all '<?php echo esc_html( $term->name ); ?>' posts
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Attached is my set up of the ACF fields
EDIT****
This was my solution
<?php
$tax = 'ingredients';
$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('image', 'ingredients_' . $term->term_id );
$description = get_field('description', 'ingredients_' . $term->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>';
echo $description;
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
?>
Try this and replace term_name_ with your actual term slug:
<?php
$terms = get_field('ingredients_selector');
if( $terms ): ?>
<ul>
<?php foreach( $terms as $term ): ?>
<li>
<h2><?php echo esc_html( $term->name ); ?></h2>
<p>Term description: <?php the_field('description', 'term_name_'.$term->term_id); ?></p>
<p>Term Image: <?php the_field('image', 'term_name_'.$term->term_id); ?></p>
<p><?php echo esc_html( $term->description ); ?></p>
View all '<?php echo esc_html( $term->name ); ?>' posts
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
You can find more about this here.
Related
I have acf option page with the field Relationship. I display this block on many pages of the site. How can I exclude the current page from the list of displayed pages?
My code for Relationship:
<?php $coin_pages = get_field('sb_sector_pages_pages', 'option');
if( $coin_pages ):
foreach( $coin_pages as $post ) :
$permalink = get_permalink( $post->ID );
$thumbnail = get_the_post_thumbnail( $post->ID, 'full');
$title = get_the_title( $post->ID );
setup_postdata($post); ?>
<li class="coin-article__sidebar-list-item">
<a href="<?php echo esc_html( $permalink ); ?>" class="coin-article__sidebar-list-link">
<div class="coin-article__sidebar-coin-ico">
<?php echo $thumbnail; ?>
</div>
<span class="coin-article__sidebar-coin-title"><?php echo esc_html( $title ); ?></span>
</a>
</li>
<?php endforeach; ?>
I tried to add this to my functions.php but it doesn’t work for me:
add_filter('acf/fields/relationship/query/name=sb_sector_pages_pages', 'exclude_id', 10, 3);
function exclude_id ( $args, $field, $post ) {
$args['post__not_in'] = array( $post );
return $args;
}
How to exclude a current page from the ACF relationship query??
I’d like to be able to display the category description for each of the categories in our shop – both parent and child – either in an overlay below the h4 category title or underneath.
Our theme has a filter which governs this layout, and any attempts I’ve made to adapt it and add an additional div or span by including echo category_description() inside the h4 title tags has been unsuccessful - [https://www.epianos.co.uk/digital-pianos/][1]
<div class="category-grid-item">
<a class="category-img" href="<?php echo esc_url( get_term_link( $c->slug, 'product_cat' ) ); ?>">
<?php
$thumbnail_id = get_term_meta( $c->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_image_src( $thumbnail_id, 'large' );
$image = isset( $image[0] ) ? $image[0] : wc_placeholder_img_src();
if ( isset( $image ) ) {
?>
<img src="<?php echo esc_url( $image ); ?>" alt="<?php echo esc_attr( $c->name ); ?>" />
<?php
}
?>
</a>
<h4 class="category-title">
<?php echo esc_html( $c->name ); ?><span class="count"><?php echo esc_attr( $c->count ); ?></span>
</h4>
</div>
[1]: https://www.epianos.co.uk/digital-pianos/
Try to pass the category id to the category_description function
from documentation:
category_description( int $category )
Retrieves category description.
$category (int) (Optional) Category ID. Defaults to the current category ID.
Your code should be as follows:
<h4 class="category-title">
<?php echo esc_html( $c->name ); ?> <?php echo category_description( $c->term_id ); ?>
<span class="count"><?php echo esc_attr( $c->count ); ?></span>
</h4>
I am trying to get the image from a category but I cannot retrieve the image.
Today I already learned that I had to use
get_field('product', $term->taxonomy . '_' . $term->term_id);
to fetch content.
But when I use this same method to fetch an image URL from an ACF Field linked to my custom post type category, I do not recieve any values.
This is my code (the var_dump is included):
<?php
$args = array(
'post_type' => 'segments-overview',
'orderby' => 'date', // we will sort posts by date
);
$query = new WP_Query( $args );
$all_terms = [];
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
$terms = get_the_terms(get_the_ID(), 'category-segments-overview');
foreach($terms as $term) $all_terms[$term->term_id] = $term;
endwhile;
foreach($terms as $term):
?>
<div class="segments-card">
<div class="img">
<?php
$image = get_field('image', $term->taxonomy . '_' . $term->term_id);
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
</div>
<div class="content">
<div class="title"><?php echo $term->name; ?></div>
<!-- <?php print_r($term); ?> -->
<a class="button transparent" href="/segments/<?php echo $term->slug; ?>">
<?php echo __('View All','axia'); ?>
</a>
</div>
</div>
</div>
<?php endforeach;
wp_reset_postdata();
else :
?>
<div class="no-posts-found">
<h2>There were no items found</h2>
<h3>Please try a different search</h3>
</div>
<?php
endif;
?>
I use this var_dump to see if everything is fetched:
$image = get_field('image', $term->taxonomy . '_' . $term->term_id);
echo '<pre>';
echo "Image field value:";
var_dump($image);
echo "Category field value:";
var_dump($term);
echo '</pre>';
The only thing is that I do not get the value from my image in my category that is made in ACF.
You can simply get value by passing term object as second parameters.
$image = get_field('image', $term);
Check the docs here: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
I can't seem to pull a ACF image object for a custom taxonomy when used in this loop, which is made of echo blocks:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$terms = get_terms( 'product_type' );
$image = get_field('product_type_tax_image');
$hero_image = $image['sizes']['medium'];
foreach ( $terms as $term ) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
var_dump($hero_image);
// We successfully got a link. Print it out.
echo '<a href="' . esc_url( $term_link ) . '" class="product-grid- block" style="' . $hero_image[0] . '">';
echo '<div class="product-grid-inner-txt-wrap">';
echo '<h4>' . $term->name . '</h4>';
echo '<p>' . $term->description . '</p>';
echo '</div>';
echo '</a>';
}
?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
The var dump just returns NULL. I've looked at this and this but don't seem to help. What am I doing wrong?
Take a look at this article:
http://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/
To pull a custom field from a Taxonomy Term, you need to add a second item to your get_field function call. Like this:
$image = get_field('product_type_tax_image', $term );
Also, it looks like you're trying to loop through $terms, but you're grabbing the custom field's value outside of your foreach loop, so that should get moved inside that loop, like so:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$terms = get_terms( 'product_type' );
foreach ( $terms as $term ) {
//get $term's image
$image = get_field('product_type_tax_image', $term );
$hero_image = $image['sizes']['medium'];
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
var_dump($hero_image);
// We successfully got a link. Print it out.
echo '<a href="' . esc_url( $term_link ) . '" class="product-grid- block" style="' . $hero_image[0] . '">';
echo '<div class="product-grid-inner-txt-wrap">';
echo '<h4>' . $term->name . '</h4>';
echo '<p>' . $term->description . '</p>';
echo '</div>';
echo '</a>';
}
?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
I'm trying to display a number of custom post taxonomy terms - but each within their own specific list item.
I have a site which is about garages. (developing on localhost so don't have a link yet).
I have a custom post type called Cars. Within this I have a custom post taxonomy with the make of the Car 'Ford' in this case.
Within 'Ford' is a list of custom post taxonomy terms of all the ford cars at that garage. 'GT', 'Sierra', 'Orion'.
Instead of listing the terms: 'GT', 'Sierra', 'Orion'. I want to show a picture of the car within a ul list.
I've created a sprite with all the images and want to loop through these setting the background position for each li item.
The first lot of code below displays a list of of the terms which is all good, but not what I want. Right at the bottom is the code which I've tried to add the list items to but just getting a blank screen...
Any help, much appreciated. Thanks
<?php if ( get_post_type() == cars ) { ?>
<div class="entry-meta-custom">
<?php
$terms = get_the_terms( $post->ID, 'ford' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->name;
}
$on_draught = join( ", ", $draught_links );
?>
<?php echo $on_draught; ?>
<?php endif; ?>
</div><!-- .entry-meta-custom -->
<?php } ?>
And here's where I've tried to add the list items.
<?php if ( get_post_type() == cars ) { ?>
<div class="entry-meta-custom">
<?php
$terms = get_the_terms( $post->ID, 'ford' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
if ($term->name == 'gt') {
$term->name = '<li class="gt">' . $term->name . '</li>';
}
if ($term->name == 'sierra') {
$term->name = '<li class="sierra">' . $term->name . '</li>';
}
if ($term->name == 'orion') {
$term->name = '<li class="orion">' . $term->name . '</li>';
}
$draught_links[] = $term->name;
}
$on_draught = join( ", ", $draught_links );
?>
<?php echo '<ul>' . $on_draught . '</ul>; ?>
<?php endif; ?>
</div><!-- .entry-meta-custom -->
<?php } ?>
I worked it out. Below is the code for anyone that is looking to replace custom post taxonomy list terms with individual images which are link to the term archive.
Source http://codex.wordpress.org/Function_Reference/get_term_link
<?php if ( get_post_type() == cars ) { ?>
<div class="entry-meta-custom">
<?php
$terms = get_terms('ford');
echo '<ul>';
foreach ($terms as $term) {
echo '<li class="'.$term->name.'"></li>';
}
echo '</ul>'; ?>
</div><!-- .entry-meta-custom -->
<?php } ?>