How to make the right PHP code link for the WP page template page-{slug}.php?

Hello, tell me please

There is the following hierarchy:
There is a page of Names with the template page-names.php
Inside it has a page of "van 1" to the template page-names.php and inside it there is a page "Ivan 2", "van 3", etc. (anywhere the pattern page-names.php).
Ie, we get the following:
Home/Names/Ваня1/Ваня2
On the page "Name" is the text link to page "van 1", respectively, where is the link to the page "Ivan 2", etc.

But, on the page "Names" can be the link to the page "Даша1" pattern page-names.php where is the link to the page "Даша2", "Dora 3", etc. (anywhere the pattern page-names.php).

Question:
How to make when finding the template page-names.php visit:
"Ivan 2", "van 3", etc. was a reference to "van 1", and when on the page "van 1" - a link to the page "Names".
Accordingly, to also was all for pages "Dora 2", "Dora 3" etc. to link to the page "Dora 1" and while on the page, "Dora 1" - a link to the page "Names".
Thanks in advance.
Interested in the PHP code for WP, because I do not overpower it.
March 19th 20 at 08:37
2 answers
March 19th 20 at 08:39
Solution
Quite a strange question, but there is a solution.
In the template using conditional tags to check which page is the user.
A simple example:
<?php

 if(is_page(1)): // ID page of Ivan
 echo '<a href="https://example.com">Dasha</a>'; // Show a link to the page Dashi
 elseif (is_page(2)): // ID page Dashi
 echo '<a href="https://example.com">Ivan</a>'; // Show a link to John's page
 elseif (is_page(3)): // ID of the page is Nasty
 echo '<a href="https://example.com">Valery</a>'; // Show a link to the page Valeria
 else: 
 echo '<a href="https://example.com">Nastya</a>';
endif;
?>

You can use switch case for beauty)
And that means
You can use switch case for beauty)
? - marietta.Parker commented on March 19th 20 at 08:42
@marietta.Parker,
https://www.w3schools.com/Php/php_switch.asp
instead of an if elseif else statement to use a switch case. Use the one which is owned - Emely.Predovic commented on March 19th 20 at 08:45
all... deal with switch case... ))) - marietta.Parker commented on March 19th 20 at 08:48
March 19th 20 at 08:41
If I correctly catch the hierarchy, just
<?php if ($post->post_parent): ?>
 <a href="<?php the_permalink($post->post_parent); ?>"><?php echo esc_html(get_the_title($post->post_parent)) ?></a> 
<?php endif; ?>

Find more questions by tags WordPress