How do I display my custom fields that are only in a specific category? I have two custom fields (residential and commercial listings) and the client runs specials on the residential properties.
I’d like to create a page separately from the residential listings that shows only the specials. I’ve dug through the documentation, but I either haven’t found what I need to do, or I just don’t understand it. Help?
ACF documentation page: Addign fields to Taxonomy terms
<?php
/**
* this should go to archive.php or category.php
* you can also target category like: category-name.php
* Get things for ACF on category
*/
$term_id = null;
$taxonomy = null;
$queried_object = get_queried_object();
$queried_term = null;
/**
* Dont show on Search or 404 page
*/
if ( !$queried_object == null && !is_search() && !is_404() ) {
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$queried_term = $taxonomy . '_' . $term_id;
}
?>
<?php if ( get_field( 'acf_true_false', $queried_term ) ) : ?>
<?php if ( get_field( 'acf_text_field', $queried_term ) ): ?>
<div>
<?php the_field( 'acf_text_field', $queried_term ); ?>
</div>
<?php endif; ?>
<?php endif; ?>
Conditional tags, to show things only on one category:
<?php if ( is_category('category_slug') ): ?>
<!-- show only on category_slug category, you can put ACF code here also -->
<?php endif; ?>
Hope it helps,