admin管理员组文章数量:1332890
I have a page template that loops through all existing post categories and outputs 5 associated posts for each category. I want to change this so it only outputs the categories I have selected in an ACF Taxonomy field on a particular page.
I have set up the page and added the ACF Taxonomy field to this, however I can't work out how to get the original loop to only use the taxonomies selected here. What's the best way to achieve this?
I've tried passing the taxonomies via get_field('taxonomy_field_name')
into the first foreach, however that doesn't seem to be working
Here's how I'm looping through the categories/posts:
<?php
/* Loop through Categories and Display Posts */
$post_type = 'post';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$args = array(
'post_type' => $post_type,
'posts_per_page' => 5, //show all posts
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args); ?>
<div class="category-container-row">
<?php if( $posts->have_posts() ): ?>
<div class="category-title">
<h2>
<?php echo '<a href="' . get_site_url() . '/' . $term->slug . '">' . $term->name . '</a>';?>
</h2>
</div>
<div class="post-listing-row carousel-container">
<?php while( $posts->have_posts() ) : $posts->the_post(); ?>
<article class="single-post">
<h3>
<a href="<?php echo get_permalink($post->ID); ?>"><?php echo get_the_title(); ?></a>
</h3>
</article>
<?php endwhile; endif; ?>
</div>
</div>
<?php endforeach;
endforeach; ?>
I have a page template that loops through all existing post categories and outputs 5 associated posts for each category. I want to change this so it only outputs the categories I have selected in an ACF Taxonomy field on a particular page.
I have set up the page and added the ACF Taxonomy field to this, however I can't work out how to get the original loop to only use the taxonomies selected here. What's the best way to achieve this?
I've tried passing the taxonomies via get_field('taxonomy_field_name')
into the first foreach, however that doesn't seem to be working
Here's how I'm looping through the categories/posts:
<?php
/* Loop through Categories and Display Posts */
$post_type = 'post';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
$args = array(
'post_type' => $post_type,
'posts_per_page' => 5, //show all posts
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args); ?>
<div class="category-container-row">
<?php if( $posts->have_posts() ): ?>
<div class="category-title">
<h2>
<?php echo '<a href="' . get_site_url() . '/' . $term->slug . '">' . $term->name . '</a>';?>
</h2>
</div>
<div class="post-listing-row carousel-container">
<?php while( $posts->have_posts() ) : $posts->the_post(); ?>
<article class="single-post">
<h3>
<a href="<?php echo get_permalink($post->ID); ?>"><?php echo get_the_title(); ?></a>
</h3>
</article>
<?php endwhile; endif; ?>
</div>
</div>
<?php endforeach;
endforeach; ?>
Share
Improve this question
edited Jul 3, 2019 at 14:52
user108167
asked Jul 3, 2019 at 14:42
user108167user108167
11 silver badge4 bronze badges
3
- Can you please share the code you already have, and what you've attempted, – Jacob Peattie Commented Jul 3, 2019 at 14:47
- Apologies, that's added now – user108167 Commented Jul 3, 2019 at 14:52
- Where do you get data from your ACF field ? we can not see that on the code u just put above. And in the tax_query, you'r passing an object 'taxonomy' => $taxonomy, instead of the name of your taxonomy! 'taxonomy' => $taxonomy->name, – Rachid Chihabi Commented Jul 3, 2019 at 15:01
1 Answer
Reset to default 1The ACF "Taxonomy" field lets you select terms from a taxonomy, not taxonomies themselves. So if you want your selections in that field to be used in your code, you need to:
- Remove the
get_object_taxonomies()
line and the subsequentforeach
. You're looping through terms, not Taxonomies, - Make sure the Taxonomy field is set to return Term ID. As the primary key, this is likely faster than querying posts by term slugs.
- Replace
get_terms()
with theget_field()
for your custom field. This is what you'll be looping over. - Change your
tax_query
to useterm_id
as the field, to match the output of the field as set in #2.
That will leave you with something like this:
$post_type = 'post';
$taxonomy = 'taxonomy_name';
$terms = get_field( 'taxonomy_field_name' );
foreach( $terms as $term ) :
$args = array(
'post_type' => $post_type,
'posts_per_page' => 5,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $term,
),
),
);
$posts = new WP_Query( $args );
// Output posts, etc.
endforeach;
本文标签: advanced custom fieldsLoop through ACF taxonomies and output associated posts
版权声明:本文标题:advanced custom fields - Loop through ACF taxonomies and output associated posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742308023a2450307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论