Answer the question
In order to leave comments, you need to log in
How to list pages with custom ACF field in Wordpress?
Good day!
Using the "Advanced Custom Fields" plugin, a custom field "County" was created. All values of this field (YuAO SWAO, ZAO, SZAO, SAO, SVAO, TsAO) are assigned to different pages.
Question:
How to show a list of all pages (title, image) with this field on a separate (new) page, in order to later attach a filter to it, in which, when choosing, for example, the CAO district, pages with the value of the CAO field were displayed or when choosing the CAO , pages with the value of the CAO field were displayed.
Answer the question
In order to leave comments, you need to log in
You will need the WP_Query() class, which allows you to create additional loops with the parameters you need. I will make the assumption that the acf-field with information about the district is called okrug.
An example of displaying all pages in ZAO:
<?php
// Задаем параметры цикла:
$args = array(
'numberposts' => -1,
'post_type' => 'page',
'meta_key' => 'okrug',
'meta_value' => 'ЗАО'
);
// С помощью WP_Query создаем переменную, содержащую все страницы со значением ЗАО в поле округ
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<img src="<?php echo get_the_post_thumbnail_url(); ?>" />
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Возвращаем в норму все глобальные переменные ?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question