0

I have like 5 parent pages and would like to echo all their title's on the front page.php. Maybe with get_the_id(); but don't know how.

  • i already answered, but i'm not sure if i do get what you actually need. you navigated to a page and you want to show its parents? than my answer is for you. if not, please clarify.. – honk31 Oct 18 '18 at 13:52
  • thanks for your answer. I have what I need :) @honk31 – Gago Oct 20 '18 at 20:00
1

Check Post & Page Parameters or get_page_children().

<?php
global $post;
$args = array(
    'post_type'      => 'page',
    'posts_per_page' => -1,
    'post_parent'    => $post->ID,
    'order'          => 'ASC',
    'orderby'        => 'menu_order'
 );


$parent = new WP_Query( $args );

if ( $parent->have_posts() ) : ?>

    <?php while ( $parent->have_posts() ) : $parent->the_post(); ?>

        <div id="parent-<?php the_ID(); ?>" class="parent-page">

            <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>

        </div>

    <?php endwhile; ?>

<?php endif; wp_reset_postdata(); ?>
  • Hey thanks for the answer. The $post->Id is empty i get nothing when i try this – Gago Oct 18 '18 at 10:31
  • Depending on how your theme/plugin is structured you could use get_the_ID() in place of $post->ID. If get_the_ID() won't work, you can grab the global $post object & retrieve the ID from it. – admcfajn Oct 18 '18 at 19:20
0

i would go for get_ancestors. it will return an array of ID's that you can use for whatever you want..

$anchestors = get_ancestors( get_the_ID(), 'page' );
if (!empty($anchestors)) :
    foreach ($anchestors as $anchestor) :
        echo get_the_title( $anchestor );
    endforeach;
endif;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.