Friday 19 June 2015

Wp Query Get Child Post Or Page Of Current Page Or Post In Wordpress(Parent Post)

Hello

I am Giving example of how to get  parent post of child post or page.

I have top market post type. i am create child post in this i have code to get child post of current parent post.

  $args = array(
    'post_type'      => 'top_markets',
    '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_query(); ?>


if you to want current page child page in wordpres check this code in your template file.

$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_query(); ?>

No comments:

Post a Comment

Thank You For Comment