1

I have a custom post type and a page like this for displaying the posts inside :

archive-(myCPT).php I've created 2 posts, and one of them is the child. How can I display just the parent page inside my archive-(myCPT).php

Here is a snippet from archive-(myCPT).php :

<?php 

        if( have_posts() ){ 
            // normal blog layout
                $x = 1;
                while ( have_posts() ){
                    the_post(); 
                    get_template_part( 'inc/post-format/content-debate');
                    // advertising between posts
                    if($ad_posts_mode != 'none'){
                        // take into account ad frequency
                        if (($x % $ad_posts_frequency) == 0){

                            switch ($ad_posts_mode) {
                                case 'image':
                                    echo '<div class="'.(($ad_posts_box) ? 'box' : '').' between_posts"><a target="_blank" href="'.of_get_option('ad_posts_image_link').'"><img src="'.of_get_option('ad_posts_image').'"></a></div>';
                                break;
                                case 'html':
                                    echo '<div class="'.(($ad_posts_box) ? 'box' : '').' between_posts">'.apply_filters('shortcode_filter',do_shortcode(of_get_option('ad_posts_code'))).'</div>';
                                break;
                            }
                        }
                    }
                    $x++;
                }
            }

1 Answer 1

5

When a post (of ony type) is a child, its property post_parent is a number with the parent’s post ID. Posts without a parent have a value 0 instead. So you can test this value:

if ( 0 === (int) $post->post_parent )
{
    // show the post
}     

Another option is a filter on pre_get_posts (not tested, just an idea):

add_action( 'pre_get_posts','hide_children' );

function hide_children( $query ) 
{
    remove_action( 'pre_get_posts', current_filter() );

    if ( is_admin() or ! $query->is_main_query() ) 
        return;

    if ( ! $query->is_post_type_archive( 'your_post_type_name' ) )
        return;

    // only top level posts
    $query->set( 'post_parent', 0 );
}
4
  • I want to try the the first option , but I don't know where exactly should I put the if statement should I put it inside the if( have_posts() ){ or ?
    – agis
    Nov 6, 2013 at 16:08
  • @Alecs Yes, use it after calling the_post();.
    – fuxia
    Nov 6, 2013 at 16:15
  • I've added after post: like this if ( 0 !== (int) $post->post_parent ) { get_template_part( 'inc/post-format/content-debate'); } but now is displaying just the child post
    – agis
    Nov 6, 2013 at 16:59
  • @Alecs Sorry, 0 must be the same. Fixed in my answer.
    – fuxia
    Nov 6, 2013 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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