How to use ACF field as part of an array
-
This is an advanced question, I’m not really sure where to start, but I’m hoping an advanced programmer might know the answer to this one.
I need to create either a plain text field or dropdown menu with an ACF field in the backend, then incorporate it into an array in the template.
The goal is to show posts on a PAGE by selecting the category (or typing the catgory slug) to show (for instance, show posts from Category One on a regular page).
This is my code:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'category_name' => 'MyCategoryChoice', 'paged' => $paged ); $list_of_posts = new WP_Query( $args ); ?> <?php if ( $list_of_posts->have_posts() ) : ?> <?php /* The loop */ ?> <?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?> <?php // Display content of posts ?> <?php get_template_part( 'content', 'home' ); ?> <?php endwhile; ?>
I need to replace the ‘MyCategoryChoice’ with a ACF field, but it’s not working.
I tried this, but it didn’t work…
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $mycategory = the_field('selected_category'); $args = array( 'category_name' => $mycategory, 'paged' => $paged );
where ‘selected_category’ is the field name… All that did was show the category name as plain text.
Any ideas out there? My brain hurts already ??
Thanks!
https://www.ads-software.com/plugins/advanced-custom-fields/
- The topic ‘How to use ACF field as part of an array’ is closed to new replies.