Answer the question
In order to leave comments, you need to log in
How to display a list of pages with a custom field on a wordpress page with a specific page ID, the value of which is equal to the ID of the current page?
Greetings!
The page structure is as follows:
Parent: Credit Cards
Pages: Card #1, Card #2, Card #3, etc.
There are pages of banks, for example, Bank #1 has page ID = 124, and some credit cards from the list have an arbitrary field, the value of which is equal to the ID of this bank, and a list of these cards should be displayed on the page of this bank.
The bank page template has the following code:
<?php
$nomer = get_the_ID();
$args = array(
'post_type' => 'page',
'meta_query' => array(
array(
'key' => 'bank_id',
'value' => $nomer,
)
),
);
$pages = get_pages( $args );
foreach( $pages as $post ){
setup_postdata( $post );
?>
<div><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
<?php
}
wp_reset_postdata();
?>
Answer the question
In order to leave comments, you need to log in
Found a solution here :
<?php
$nomer = get_the_ID(); // Определяем ID страницы на которой находимся
// Задаем параметры цикла:
$args = array(
'numberposts' => -1,
'post_type' => 'page',
'meta_key' => 'bank_id',
'meta_value' => $nomer
);
// С помощью WP_Query создаем переменную, содержащую все страницы со значением ЗАО в поле округ
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
<?php endwhile; ?>
<?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