Home › Forums › General Issues › Query in a taxonomy field
Hey!
I would like to get a list of categories with:
– the name of the category,
– a link to the category,
– the two first posts within a query.
I am using the Taxonomy field with checkboxes.
I manage to get the title and the link with the code below:
<?php
$terms = get_field('categories_a_afficher', 'option');
if( $terms ): ?>
<?php foreach( $terms as $term ): ?>
<div class="category_list">
<h2 class="fond">
<?php echo $term->name; ?>
</h2>
<a href="<?php echo get_term_link( $term ); ?>">
View all <strong><?php echo $term->name; ?></strong> posts
</a>
<!-- Here the query begin -->
</div><!-- .category_list -->
<?php endforeach; ?>
<?php endif; ?>
Still, I don’t get how to query posts in there.
I use the Taxonomy field with checkboxes, so I put this on my function.php:
resources/acf-fields-taxonomy-wp_list_categories/
function my_taxonomy_wp_list_categories( $args, $field ) {
// modify args
$args['orderby'] = 'count';
$args['order'] = 'ASC';
// return
return $args;
}
add_filter('acf/fields/taxonomy/wp_list_categories', 'my_taxonomy_wp_list_categories', 10, 2);
But how am I supposed to do make the query?
Thanks in advance.
Nevermind, I find a way.
Here is my code in case someone would do it:
<?php
$terms = get_field('categories_a_afficher', 'option');
if( $terms ): ?>
<?php foreach( $terms as $term ): ?>
<div class="category_list">
<h2 class="fond">
<?php echo $term->name; ?>
</h2>
<a href="<?php echo get_term_link( $term ); ?>">
<?php _e( 'View all posts from', 'twentyeleven' ); ?> <strong><?php echo $term->name; ?></strong>
</a>
<!-- Here the query begin -->
<?php query_posts( 'showposts=2&order=DSC&cat=' . $term->term_id . ''); if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; endif; wp_reset_query(); // On ferme la boucle ?>
<?php //echo $term->term_id; ?>
</div><!-- .category_list -->
<?php endforeach; ?>
<?php endif; ?>
The topic ‘Query in a taxonomy field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.